async function test1() {
console.log('test1 begin');
const result = await test2();
console.log('result', result);
console.log('test1 end');
}
async function test2(){ // 异步函数默认返回一个 Promise
console.log('test2');
return Promise.reslove('haha')
}
console.log('script begin');
test1();
console.log('script end');
输出
script begin
test1 begin
test2
script end
result haha
test1 end
可以把 await后面的代码当成一个 setTimeout(()=>{}) 包裹住(是异步), 以便理解