【发布时间】:2018-09-03 13:43:00
【问题描述】:
我是 Async 和 await 生态系统的新手,但我知道它提供了一种同步方式的编码方式(尽管它在幕后是异步的,只是用代码编写的方式)。
这是我想要以异步方式执行的代码。
const axios = require("axios");
async function getJSONAsync(){
// The await keyword saves us from having to write a .then() block.
let json = await axios.get('https://tutorialzine.com/misc/files/example.json');
console.log('after the call to service');
// The result of the GET request is available in the json variable.
// We return it just like in a regular synchronous function.
return json;
}
let abc = getJSONAsync();
console.log('>>>>>>>>>>> abc', abc);
现在有一些我无法破解的查询,让我们先看看输出:
>>>>>>>>>>> abc Promise { <pending> }
after the call to service
- 该行是在调用服务之后在执行之后出现的。为什么?异步等待行为发生了什么?
请发表一些看法?
提前致谢,祝您编码愉快:)。
【问题讨论】:
标签: node.js async-await axios