new Promise((resolve, reject) => {
resolve()
}).then(() => {
console.log(1)
new Promise((resolve, reject) => {
resolve()
}).then(() => {
console.log(2)
})
setTimeout(() => { console.log(3) }, 0)
})
console.log(4)
setTimeout(() => { console.log(5) }, 0)
// 4
// 1
// 2
// 5
// 3
Why print 2 to 5 first? After the first macrotask queue
execution, push out the function in Promise.then in microtask queue
. After execution, shouldn"t you call setTimeout
in macrotask queue
? Why the Promise in the. Then is executed first.