zxdfe / FE-Interview

Every step counts
34 stars 1 forks source link

第21题:async-promise相关异步知识的考察,必会! #21

Open zxdfe opened 2 years ago

zxdfe commented 2 years ago
Moooodena commented 2 years ago

script start async1 start async2 promise1 script end async1 end promise2 setTimeout

A-Lonely-GEEK commented 2 years ago
   'script start'
    async1 start
    async2
    promise1
    script end
    async1 end
    promise2
    setTimeout
szgyFE commented 1 year ago

首先是同步任务依次执行 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'); 结果如上