繁体   English   中英

NodeJS获取承诺回调挂起

[英]NodeJS fetch promise callback pending

我有这个代码:

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完成时才能运行该函数。 我该怎么做呢?

response.json()返回一个promise, then使用callback来获取数据:

Body mixin的json()方法接受一个Response流并将其读取完成。 它返回一个promise,它以将正文解析为JSON的结果解析。

fetch(url)
  .then(response => response.json())
  .then(data => console.log('simplest possible fetch', data, data.where));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM