fetch 函数返回 Promise <pending> [英] fetch function return Promise <pending>

查看:20
本文介绍了fetch 函数返回 Promise <pending>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的代码在这里返回一个 Promise,因为我使用的是 then 语法,所以我不知道为什么会这样:-??

So my code here return a Promise and since I'm using then syntax I don't know why that happens :-??

fetch('someurltoAJsonFile.json')
  .then(function(response) {
    console.log(response.json());});

推荐答案

node-fetch 库中的response.json() 也返回一个promise,改为尝试

response.json() in node-fetch library also returns a promise, instead try

fetch('someurltoAJsonFile.json')
  .then(response => response.json())
  .then(data => {
    console.log(data)
  });

您可以在这里

返回的响应似乎不在有效的 json 中,所以为了完整起见,这里是文本代码

It seems that the returned response wasn't in the valid json, so for the sake of completeness here is a code for text

fetch('someurltoAJsonFile.json')
  .then(response => response.text())
  .then(data => {
    console.log(data)
  });

这篇关于fetch 函数返回 Promise <pending>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆