回调队列中的任务称之为[宏任务],宏任务执行过程中可以临时加上一些额外需求.可以选择作为一个新的宏任务进到队列中排队.也可以作为当前任务的[微任务],直接在当前任务结束过后立即执行.
微任务:本轮结束后立即执行 ,提高了整体的响应能力
注意:
- 目前绝大多数异步调用都是作为宏任务执行
- 微任务举例: Promise MutationObserver process.nextTick
测试:
console.log('global start');
setTimeout(()=>{
console.log('setTimeOut');
},0)
Promise.resolve().then(()=>{
console.log('promise');
}).then(() => {
console.log('promise 2');
}).then(()=>{
console.log('promise 3');
})
console.log('global end');
测试结果: