美文网首页
Promise执行时序

Promise执行时序

作者: 湘兰沅芷 | 来源:发表于2021-11-04 23:06 被阅读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

相关文章

网友评论

      本文标题:Promise执行时序

      本文链接:https://www.haomeiwen.com/subject/bjrlzltx.html