同步代码先执行,微任务优先于宏任务。

本节要点

  • Promise.then 属于微任务
  • setTimeout 属于宏任务
  • 每次宏任务后清空微任务队列

代码示例

console.log("1")

setTimeout(() => console.log("2"), 0)

Promise.resolve().then(() => console.log("3"))

console.log("4")

// 输出:1 4 3 2