我对异步和等待技术还不是很熟悉,但我知道它可以让我们以同步的方式编写代码(虽然在幕后是异步的,只是在代码中以同步的方式呈现)。
下面是我想要以异步方式完成的代码:
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
- 在调用服务后,这行代码出现在执行之后。为什么?async-await的行为发生了什么变化?
请分享一些看法。
提前感谢,祝编码愉快 :)。