1.由宿主环境发起的称为宏观任务
2.由javascript引擎发起的称为微观任务
宏观任务有:
浏览器 | node | |
---|---|---|
setTimeout | ✔️ | ✔️ |
setInterval | ✔️ | ✔️ |
setImmediate | ✘ | ✔️ |
requestAnimationFrame | ✔️ | ✘ |
微观任务有:
浏览器 | node | |
---|---|---|
process.nextTick | ✘ | ✔️ |
MutationObserver | ✔️ | ✘ |
Promise | ✔️ | ✔️ |
setTimeout(()=>console.log(1));
const promise = new Promise((resolve, reject) => {
resolve()
console.log(2)
})
promise.then(() => {
console.log(3)
})
console.log(4);
//执行结果
//2
//4
//3
//1