macro-task(宏任务):
- 整体代码script、
- setTimeout、
- setInterval、
- I/O、
- UI交互事件,每次执行栈执行的代码是一个宏任务;
micro-task(微任务):
- Promise,
- process.nextTick,
- 且process.nextTick优先级大于promise.then。可以理解是在当前 task 执行结束后立即执行的任务
console.log('1');
async function async1() {
console.log('2');
await async2();
console.log('3');
}
async function async2() {
console.log('4');
}
process.nextTick(function() {
console.log('5');
})
async1();
new Promise(function(resolve) {
console.log('10');
resolve();
}).then(function() {
console.log('11');
});
console.log('12');
setTimeout(function() {
console.log('6');
process.nextTick(function() {
console.log('7');
})
new Promise(function(resolve) {
console.log('8');
resolve();
}).then(function() {
console.log('9')
})
})