有人可以解释为什么承诺'then'函数在setTimeout函数之前触发?我原以为setTimeout函数会先在事件循环上调度,所以它会在promise.then函数之前运行。
setTimeout(() => {
console.log('timer done, thought this would print second')
}, 0)
Promise.resolve().then(() => {
console.log('promise done, thought this would print third')
})
console.log('synchronous, should print first')
输出:
'同步,应首先打印'
'承诺完成,认为这将打印第三个'
'计时器完成,认为这会打印第二个'