async和await

async和await示例

为什么异步执行?

不影响UI渲染

为什么await?

函数执行前后顺序保证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('3.resolved');
}, 2000);
});
}

async function asyncCall() {
console.log('1.calling');
// 阻塞等待resolveAfter2Seconds函数返回的Promise解析完成
var result = await resolveAfter2Seconds();
console.log(result);
// expected output: 'resolved'
}

asyncCall(); // 直接调用
console.log("2.hello");

// 等待所有任务执行完成才结束 exit(0)