宏任务、微任务有哪些?案例

88 阅读3分钟
宏任务、微任务有哪些?

宏任务:1. script (可以理解为外层同步代码)2. setTimeout/setInterval3. UI rendering/UI事件4. postMessage,MessageChannel5. setImmediate,I/O(Node.js)

微任务:1. Promise2.process.nextTick(Node.js) 3. Object.observe(已废弃;Proxy 对象替代)4. MutaionObserver

宏任务、微任务是怎么执行的?

执行顺序:先执行同步代码,遇到异步宏任务则将异步宏任务放入宏任务队列中,遇到异步微任务则将异步微任务放入微任务队列中,当所有同步代码执行完毕后,再将异步微任务从队列中调入主线程执行,微任务执行完毕后再将异步宏任务从队列中调入主线程执行,一直循环直至所有任务执行完毕。

这里容易产生一个错误的认识:就是微任务先于宏任务执行。实际上是先执行同步任务,异步任务有宏任务和微任务两种,先将宏任务添加到宏任务队列中,将宏任务里面的微任务添加到微任务队列中。所有同步执行完之后执行异步,再将异步微任务从队列中调入主线程执行,微任务执行完毕后再将异步宏任务从队列中调入主线程执行。之后就一直循环...

案例

例一、

setTimeout(function(){
	console.log('1');
});
new Promise(function(resolve){		    
	console.log('2');
	resolve();
}).then(function(){		    
	console.log('3');
}).then(function(){
console.log('4')
}); 		
console.log('5');
// 2 5 3 4 1

分析:

1.遇到setTimout,异步宏任务,放入宏任务队列中

2.遇到new Promise,new Promise在实例化的过程中所执行的代码都是同步进行的,所以输出2

3.而Promise.then中注册的回调才是异步执行的,将其放入微任务队列中

4.遇到同步任务console.log(‘5’);输出5;主线程中同步任务执行完

5.从微任务队列中取出任务到主线程中,输出3、 4,微任务队列为空

6.从宏任务队列中取出任务到主线程中,输出1,宏任务队列为空

例二、

setTimeout(()=>{
  new Promise(resolve =>{
  	resolve();
  }).then(()=>{
  	console.log('test');
  });

  console.log(4);
});

new Promise(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,微任务队列为空

例三、

console.log('1');

setTimeout(function() {
    console.log('2');
    process.nextTick(function() {
        console.log('3');
    })
    new Promise(function(resolve) {
        console.log('4');
        resolve();
    }).then(function() {
        console.log('5')
    })
})
process.nextTick(function() {
    console.log('6');
})
new Promise(function(resolve) {
    console.log('7');
    resolve();
}).then(function() {
    console.log('8')
})

setTimeout(function() {
    console.log('9');
    process.nextTick(function() {
        console.log('10');
    })
    new Promise(function(resolve) {
        console.log('11');
        resolve();
    }).then(function() {
        console.log('12')
    })
})
// 1 7 6 8 2 4 3 5 9 11 10 12