我有这个代码:
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
我明白了most的时间。有时我会获得“成功”状态。对我来说,这意味着回调正在 fetch Promise 解决之前运行。
我希望该函数仅在获取完成时运行。我该怎么做呢?
response.json()
返回一个承诺,使用then
回调获取数据:
Body mixin 的 json() 方法接受一个响应流并读取它完成。它返回一个承诺,该承诺将正文文本解析为 JSON 的结果进行解析。
fetch(url)
.then(response => response.json())
.then(data => console.log('simplest possible fetch', data, data.where));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)