-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
请看题:
async function async1() {
console.log('async1 start');
await async2();
console.log('async1 end');
}
async function async2() {
console.log('async2');
}
console.log('script start');
setTimeout(function() {
console.log('setTimeout');
}, 0);
async1();
new Promise(function(resolve) {
console.log('promise1');
resolve();
}).then(function() {
console.log('promise2');
});
console.log('script end');
一、await 后面的表达式
-
非
Promise
实例。
如果不是promise
,await
会阻塞后面的代码,先执行async
外面的同步代码,同步代码执行完,再回到async
内部,把这个非promise
的东西,作为await
表达式的结果。 -
Promise
实例。
如果它等到的是一个promise
对象,await
也会暂停async
后面的代码,先执行async
外面的同步代码,等着Promise
对象fulfilled
,然后把resolve
的参数作为await
表达式的运算结果。
二、宏任务/微任务队列
三、答案
script start
async1 start
async2
promise1
script end
promise2
async1 end
setTimeout
四、相关文章
Metadata
Metadata
Assignees
Labels
No labels