You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
setTimeout(()=>{newPromise(resolve=>{resolve();}).then(()=>{console.log('test');});console.log(4);});newPromise(resolve=>{resolve();console.log(1)}).then(()=>{console.log(3);Promise.resolve().then(()=>{console.log('before timeout');}).then(()=>{Promise.resolve().then(()=>{console.log('also before timeout')})})})console.log(2);
1.遇到setTimeout,异步宏任务,将() => {console.log(4)}放入宏任务队列中;
2.遇到new Promise,new Promise在实例化的过程中所执行的代码都是同步进行的,所以输出1;
3.而Promise.then中注册的回调才是异步执行的,将其放入微任务队列中
4.遇到同步任务console.log(2),输出2;主线程中同步任务执行完
5.从微任务队列中取出任务到主线程中,输出3,此微任务中又有微任务,Promise.resolve().then(微任务a).then(微任务b),将其依次放入微任务队列中;
6.从微任务队列中取出任务a到主线程中,输出 before timeout;
7.从微任务队列中取出任务b到主线程中,任务b又注册了一个微任务c,放入微任务队列中;
8.从微任务队列中取出任务c到主线程中,输出 also before timeout;微任务队列为空
9.从宏任务队列中取出任务到主线程,此任务中注册了一个微任务d,将其放入微任务队列中,接下来遇到输出4,宏任务队列为空
10.从微任务队列中取出任务d到主线程 ,输出test,微任务队列为空,结束~
控制台测试输出:1 2 3 before timeout also before timeout 4 test
The text was updated successfully, but these errors were encountered:
宏任务
微任务
宏任务、微任务的执行顺序
示例
例1
例2
The text was updated successfully, but these errors were encountered: