Javascript 承诺仍在等待,然后阻止-如何解决?
我有类似的代码-Javascript 承诺仍在等待,然后阻止-如何解决?,javascript,typescript,fetch,isomorphic-javascript,isomorphic-fetch-api,Javascript,Typescript,Fetch,Isomorphic Javascript,Isomorphic Fetch Api,我有类似的代码- fetch(`${URL}${PATH}`) .then(res => { const d = res.json(); console.log("data is: ", d); return d; }) 它记录的数据是:Promise{} 如何在下一个代码语句中查看结果并加以利用 其他问题和答案建议使用then block来解决,但我仍然看到它没有解决。res.json()是异步的。你需要使用一个额外的。然后才能
fetch(`${URL}${PATH}`)
.then(res => {
const d = res.json();
console.log("data is: ", d);
return d;
})
它记录的数据是:Promise{}
如何在下一个代码语句中查看结果并加以利用
其他问题和答案建议使用then block来解决,但我仍然看到它没有解决。res.json()
是异步的。你需要使用一个额外的。然后才能得到结果
fetch(`${URL}${PATH}`)
.then(res => res.json())
.then(d => {
console.log('data is: ', d);
return d;
});
如果你得到了这种类型的值,
承诺{}
。永远记得解决它因此,您的查询将解析为
fetch(`${URL}${PATH}`)
.then(res => res.json())
.then(console.log)
.catch(console.error)
为了更好地理解,您可以利用异步/await功能。上述代码将简化为-
try{
const res = await fetch(`${URL}${PATH}`)
const dataAsJson = await res.json()
console.log(data)
}
catch(ex) {
console.error(ex)
}
res.json()
也是异步的。请看这里的第一个示例:。这建议然后来解决,但我的问题即使在then块中也是挂起的。你读过泰勒的评论吗?您需要在res.json()中处理承诺aswell@Prakharfetch
需要两个然后
sfetch()->then->result.json()->then->processresult
。好吧,我是新来的。我现在知道了。这与graphql请求
-返回请求(this.graphql_端点,查询,变量)的工作方式是否不同。然后(res=>{console.log('data is:',res);return res;})
显然,我现在正在挂起它?如何在调试器中解决?