mic*_*ech 17 javascript json asynchronous
我刚刚开始学习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数据?
谁能请解释这里发生了什么?
我真的很感激.
dee*_* zg 16
因为response.json()返回另一个promise(在你的函数体内)
https://developer.mozilla.org/en-US/docs/Web/API/Body/json
| 归档时间: |
|
| 查看次数: |
24970 次 |
| 最近记录: |