admin管理员组文章数量:1319014
I have this code:
fetch(url).then(response => {
const json = response.json();
console.log('simplest possible fetch', json, json.where);
});
In the console I get:
simplest possible fetch Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined} undefined
I get this most of the time. Sometimes I get the "success" status. To me this implies the callback is being run before the fetch promise has resolved.
I want the function to be run only when the fetch pletes. How do I do this?
I have this code:
fetch(url).then(response => {
const json = response.json();
console.log('simplest possible fetch', json, json.where);
});
In the console I get:
simplest possible fetch Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined} undefined
I get this most of the time. Sometimes I get the "success" status. To me this implies the callback is being run before the fetch promise has resolved.
I want the function to be run only when the fetch pletes. How do I do this?
Share Improve this question edited Sep 18, 2017 at 9:31 alexmac 19.6k7 gold badges64 silver badges74 bronze badges asked Sep 18, 2017 at 9:25 ShorelineShoreline 8121 gold badge11 silver badges28 bronze badges1 Answer
Reset to default 11response.json()
returns a promise, use then
callback to get the data:
The json() method of the Body mixin takes a Response stream and reads it to pletion. It returns a promise that resolves with the result of parsing the body text as JSON.
fetch(url)
.then(response => response.json())
.then(data => console.log('simplest possible fetch', data, data.where));
本文标签: javascriptNodeJS fetch promise callback pendingStack Overflow
版权声明:本文标题:javascript - NodeJS fetch promise callback pending - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742050950a2418060.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论