Promise执行时序

2021-11-04  本文已影响0人  湘兰沅芷

回调队列中的任务称为宏任务
宏任务执行过程中可以临时加上一些额外需求,可以选择作为一个新的宏任务进入到队列中排队,也可以作为当前任务的微任务,直接在当前任务结束过后立即执行。
Promise的回调会作为微任务执行

微任务会提高整体的响应能力
目前绝大多数异步调用都是作为宏任务执行
Promise & MutationObserver & process.nextTick 作为微任务在本轮调用的末尾被执行

console.log('global start')

setTimeout(() => {
  console.log('setTimeout')
}, 0)

Promise.resolve()
  .then(() => {
    console.log(1)
  })
  .then(() => {
    console.log(2)
  })
  .then(() => {
    console.log(3)
  })

  console.log('global end')
// global start
// global end
// 1
// 2
// 3
// undefined
// setTimeout
上一篇 下一篇

猜你喜欢

热点阅读