我有这个代码:
fetch(url).then(response => {
const json = response.json();
console.log('simplest possible fetch', json, json.where);
});
在控制台中我得到:
simplest possible fetch Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined} undefined
我大多数都得到了这个。有时我会获得“成功”状态。对我来说,这意味着回调正在 fetch promise 解决之前运行。
我希望函数仅在提取完成时运行。我该怎么做?
最佳答案
response.json()
返回一个promise,使用then
回调获取数据:
The json() method of the Body mixin takes a Response stream and reads it to completion. 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));
关于javascript - NodeJS 获取 promise 回调挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46275859/