【发布时间】:2019-06-16 02:16:20
【问题描述】:
我有类似的代码 -
fetch(`${URL}${PATH}`)
.then(res => {
const d = res.json();
console.log("data is: ", d);
return d;
})
它记录data is: Promise { <pending> }。
如何查看结果并在下一个代码语句中使用?
其他问题和答案建议使用 then 块来解决,但我仍然看到它未解决。
【问题讨论】:
-
res.json()也是异步的。请参阅此处的第一个示例:MDN: Using Fetch。 -
这建议
then解决,但我的即使在那个块中也是未决的。 -
你读过 Tylers 的评论吗?你需要处理
res.json()中的承诺aswell -
@Prakhar
fetch需要 两个thens。fetch() -> then -> result.json() -> then -> process result. -
好吧 - 我是这个世界的新手。我现在明白了。
标签: javascript typescript fetch isomorphic-javascript isomorphic-fetch-api