【Promise执行顺序】

记录一下对于 Promise 执行顺序的学习。

主要针对多个 then 嵌套的执行顺序

题目一

new Promise(resolve => {
  resolve()
}).then(() => {
  new Promise(resolve => {
    resolve()
  }).then(() => {
    console.log(1)
  }).then(() => {
    console.log(2)
  })
}).then(() => {
  console.log(3)
})
// 答案 1 3 2

/**
  解析
*/

** 第一次执行 **
** 此时外层的第二个 then 由于第一个 then没有执行完,因此第二个 then 不会执行 **
// 任务队列:[]
// 微任务: [
 (() => {
   new Promise(resolve => {
    resolve()
    }).then(() => {
      console.log(1)
    }).then(() => {
      console.log(2)
    })
  }).then(() => {
    console.log(3)
 })
]

** 第二次执行 **
// 任务队列:[
  (() => {
   new Promise(resolve => {
    resolve()
    }).then(() => {
      console.log(1)
    }).then(() => {
      console.log(2)
    })
  }).then(() => {
    console.log(3)
 })
]
// 微任务: []

** 执行后 **
微任务: [
	(() => {
      console.log(1)
    }).then(() => {
      console.log(2)
    }),
    (() => {
      console.log(3)
    })
]

** 第三次执行 **
任务队列: [
	(() => {
      console.log(1)
    }).then(() => {
      console.log(2)
    })
]

微任务: [
	(() => {
      console.log(3)
    })
]

** 执行后 **
微任务: [
    (() => {
      console.log(3)
    }),
    (() => {
      console.log(2)
    })
]
控制台: 1

** 第四次执行 **
第三次执行后,依次将微任务队列中的任务加入任务队列中。最后输出 1 3 2

题目二

new Promise(resolve => {
  resolve()
}).then(() => {
  new Promise(resolve => {
    resolve()
  }).then(() => {
    console.log(1)
  }).then(() => {
    console.log(2)
  })
  // 与 题目一 的 区别
  return Promise.resolve()
}).then(() => {
  console.log(3)
})
// 答案 1 2 3

/**
  解析
*/
**第一次执行**
任务队列: []
微任务: [
	(() => {
  		new Promise(resolve => {
    		resolve()
  		}).then(() => {
    		console.log(1)
  		}).then(() => {
    		console.log(2)
  		})
  		return Promise.resolve()
		}).then(() => {
  			console.log(3)
		})
]

**第二次执行**
任务队列: [
	(() => {
  		new Promise(resolve => {
    		resolve()
  		}).then(() => {
    		console.log(1)
  		}).then(() => {
    		console.log(2)
  		})
  		return Promise.resolve()
		}).then(() => {
  			console.log(3)
		})
]
微任务: []

** 执行后 ** 
微任务: [
	console.log(1), 
	Promise.resolve().then(() => { console.log(3) }),
	(() => {
    		console.log(2)
  	})
]

** 第三次执行 **
任务队列: [ console.log(1) ],
微任务: [
	Promise.resolve().then(() => { console.log(3) }),
	(() => {
    		console.log(2)
  	})
]
** 执行后 **
任务队列: [],
微任务: [
	Promise.resolve().then(() => { console.log(3) }),
	(() => {
    		console.log(2)
  	})
]
控制台: [1]

** 第四次执行 **
任务队列: [
	Promise.resolve().then(() => { console.log(3) }),
],
微任务: [ 
	(() => {
    		console.log(2)
  	})
 ]
** 执行后 **
任务队列: [],
微任务: [
	(() => {
    		console.log(2)
  	}),
  	().then(() => console.log(3))
]

** 后续执行 **
后续执行贼就是按顺序,放入任务队列,再依次输出 2 3
  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值