我刚刚开始学习Fetch API:https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
这是我编写的代码片段,用来修补它:
fetch('http://swapi.co/api/people/1')
.then(function(response) {
var json = response.json();
console.log(json);
// Expected : { "name": "Luke Skywalker","height": "1.72 m", ... }
// Get : Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
});
Run Code Online (Sandbox Code Playgroud)
我本来希望从response.json()中获取一个JSON对象.
与使用JSON.parse()时获得的内容类似.
相反,我得到了一个承诺对象.
如果我扩大这里显示的承诺链......
return response.json().then(function(json) {
// process your JSON further
});
Run Code Online (Sandbox Code Playgroud)
...然后它工作:在以下承诺的then方法中,它显示为json.
为什么我不能在第一个promise的then()中检索JSON数据?
谁能请解释这里发生了什么?
我真的很感激.