ajax、setTimeout、setInterval、DOM监听、UI Rendering Promise的then回调、 Mutation Observer API、queueMicrotask()等setTimeout(function () { console.log("setTimeout1"); new Promise(function (resolve) { resolve(); }).then(function () { new Promise(function (resolve) { resolve(); }).then(function () { console.log("then4"); }); console.log("then2"); });});new Promise(function (resolve) { console.log("promise1"); resolve();}).then(function () { console.log("then1");});setTimeout(function () { console.log("setTimeout2");});console.log(2);queueMicrotask(() => { console.log("queueMicrotask1")});new Promise(function (resolve) { resolve();}).then(function () { console.log("then3");});promise1 2then1 queueMicrotask1 then3setTimeout1,在第一个定时器中,又遇到了微任务,那么接着执行微任务then2 然后输出 then4setTimeout2promise1 2 then1 queueMicrotask1 then3 setTimeout1 then2 then4 setTimeout2async function async1() { console.log('async1 start') // await异步函数的返回结果 resolve的结果会作为整个异步函数的promise的resolve结果->同步代码 // await后面的执行代码 就会变成.then后面的执行函数->微任务 // 也就是说 console.log('async1 end') 这一段是相当于then方法内的 会被加入微任务中 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')script start async1 start async2 promise1 script endasync1 end promise2setTimeoutscript start async1 start async2 promise1 script end async1 end promise2 setTimeoutPromise.resolve().then(() => { console.log(0); //1.直接返回4 微任务不会做任何延迟 // return 4 //2.直接返回Promise.resolve(4) 微任务推迟两次 // return Promise.resolve(4); //3.返回thenable对象 return { then: ((resolve, reject) => { resolve(4); }) }}).then((res) => { console.log(res)})Promise.resolve().then(() => { console.log(1);}).then(() => { console.log(2);}).then(() => { console.log(3);}).then(() => { console.log(5);}).then(() => { console.log(6);})这道面试题有些特殊,需要大家记住两个结论
thenable对象,那么微任务会推迟一次,thenable对象就是实现了Promise.then的那个函数,具体可看代码Promise.resolve(4),那么微任务会推迟两次,这个相当于是返回一个Promise之后又用了resolve,二者是等价的本道题是基于node的事件循环,和浏览器的事件循环不一样,需要记住以下几点
node的事件循环也分宏任务和微任务
setTimeout、setInterval、IO事件、setImmediate、close事件Promise的then回调、process.nextTick、queueMicrotasknode的每次事件循环都是按照以下顺序来执行的
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('setTimeout0')}, 0)setTimeout(function () { console.log('setTimeout2')}, 300)setImmediate(() => console.log('setImmediate'));process.nextTick(() => console.log('nextTick1'));async1();process.nextTick(() => console.log('nextTick2'));new Promise(function (resolve) { console.log('promise1') resolve(); console.log('promise2')}).then(function () { console.log('promise3')})console.log('script end')script start async1 start async2 promise1 promise2 script endnextTick这个微任务nextTick1 nextTick2async1 end promise3setTimeout0 setImmediatesetTimeout2script start async1 start async2 promise1 promise2 script end nextTick1 nextTick2 async1 end promise3 setTimeout0 setImmediate setTimeout2本文来自博客园,作者:CodeSpirit,转载请注明原文链接:https://www.cnblogs.com/codespirit-zx/p/15984929.html