任务队列分为同步任务,宏任务(定时器),微任务(Promise) 执行优先级:同步>异步,微任务>宏任务 执行顺序:按照加入队列的顺序
console.log(1); // 1.第一个同步任务
setTimeout(() => {
console.log(2); // 7.第一个宏任务
Promise.resolve().then(() => {
console.log(3) // 8.第一个宏任务中的微任务
});
});
new Promise((resolve, reject) => {
console.log(4) // 2.第二个同步立即执行的同步任务
resolve(5)
}).then((data) => {
console.log(data); // 4. 第一个微任务
Promise.resolve().then(() => {
console.log(6) // 5. 第二个微任务
}).then(() => {
console.log(7) // 6.第三个微任务
setTimeout(() => {
console.log(8) //10. 第三个宏任务
}, 0);
});
})
setTimeout(() => {
console.log(9); // 9. 第二个宏任务
})
console.log(10); // 3.同步任务