promise 事件循环

新手上路,请多包涵

路过的大佬,下面的执行顺序为什么是 0 1 2 3 4 5 6,而不是 0 1 4 2 3 5 6啊,救救孩子吧😭

Promise.resolve().then(() => {
  console.log(0)
  return Promise.resolve(4);
}).then((res) => {
  console.log(res)
})


Promise.resolve().then(() => {
  console.log(1)
}).then(() => {
  console.log(2)
}).then(() => {
  console.log(3)
}).then(() => {
  console.log(5)
}).then(() => {
  console.log(6)
})
阅读 2.9k
2 个回答
Promise.resolve().then(() => {
  console.log(0)
  return 4;// 改成这样就是 0 1 4 2 3 5 6 了。
  //return Promise.resolve(4);
}).then((res) => {
  console.log(res)
})

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

问题在于 return Promise.resolve(4); 是返回的 Promise 对象 导致被延后了。
你 return 4;
就是 0 1 4 2 3 5 6 了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题