获取功能返回Promise< pending>

获取功能返回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)
  });