Open zxdfe opened 2 years ago
script start async1 start async2 promise1 script end async1 end promise2 setTimeout
'script start'
async1 start
async2
promise1
script end
async1 end
promise2
setTimeout
首先是同步任务依次执行 console.log('script start'); async1():调用函数执行 console.log('async1 start')和 await async2() awiat会阻塞往下执行,所以执行console.log('async2');继续往下执行同步代码console.log('promise1'); console.log('script end');然后执行await后面的 console.log('async1 end');最后 then 为微任务 先执行console.log('promise2');,setTimeout为宏任务,后执行console.log('setTimeout'); 结果如上