【问题标题】:?Why with multiple promises their respective thens are not are not glued together but intertwined instead??为什么有多个promise,它们各自的then不是没有粘在一起而是交织在一起的?
【发布时间】:2020-06-14 23:43:39
【问题描述】:

当你运行这段代码时

setTimeout(function(){
  console.log('setTimeout')
},0)

console.log('start');

async function first() {
  await console.log('first_resolve');
  await console.log('first_then_1');
  await console.log('first_then_2');
}
async function second() {
  await console.log('second_resolve');
  await console.log('second_then_1');
  await console.log('second_then_2');
}

first()
second()


Promise.resolve(console.log('third_resolve'))
  .then(() => console.log('third_then_1'))
  .then(() => console.log('third_then_2'))
Promise.resolve(console.log('fourth_resolve'))
  .then(() => console.log('fourth_then_1'))
  .then(() => console.log('fourth_then_2'))

console.log('end');

你在控制台中得到这个

start
first_resolve
second_resolve
third_resolve
fourth_resolve
end
first_then_1
second_then_1
third_then_1
fourth_then_1
first_then_2
second_then_2
third_then_2
fourth_then_2
setTimeout

对于带有 then 链的简单 Promise 也是如此。

我确实理解为什么 setTimeout 在最后(它是一个宏任务,宏任务必须等待微任务)。

但是 Promise 和 then 都是微任务。那么问题来了,为什么有多个 promise,它们各自的 then 不是粘在一起而是交织在一起的呢?

如我所料,输出如下:

start
first_resolve
first_then_1
first_then_2
second_resolve
second_then_1
second_then_2
third_resolve
third_then_1
third_then_2
fourth_resolve
fourth_then_1
fourth_then_2
end
setTimeout

【问题讨论】:

  • 除了setTimeout,这里没有异步代码。将 async / await 扔在某物前面并不会隐含地使其异步
  • 嗯,这基本上是一个承诺。如果你用 then 链在 promise 中重写它,你会得到相同的结果。
  • 因为这就是异步函数的作用——您可以同时运行多个异步函数。如果您想要提供的信息,您需要 .then 链接您的第一个调用,如 first().then(...) 或者您需要在函数前面放置适当的 await。
  • "对于带有 then 链的简单 Promise 也是如此。" - 你能告诉我们你的代码吗?因为这对我有用:first().then(() => second())
  • 我想你会在这里的描述部分找到答案~developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

标签: javascript promise async-await


【解决方案1】:

只有当两个异步函数 first 和 second 被顺序调用时,才会出现预期结果。这可以证明如下......

setTimeout(function(){
  console.log('setTimeout')
},0)

console.log('start');

async function first() {
  await console.log('first_resolve');
  await console.log('first_then_1');
  await console.log('first_then_2');
}
async function second() {
  await console.log('second_resolve');
  await console.log('second_then_1');
  await console.log('second_then_2');
}

async function opExperiment() {
  await first()
  await second()
  console.log('end');
}

opExperiment()

编辑

响应 OP 的编辑。异步函数之后的两块承诺代码遵循相同的规则。要让这些 Promise 顺序运行,它们必须等待或由 then() 连接。再次示范……

setTimeout(function() {
  console.log('setTimeout')
}, 0)

console.log('start');

async function first() {
  await console.log('first_resolve');
  await console.log('first_then_1');
  await console.log('first_then_2');
}
async function second() {
  await console.log('second_resolve');
  await console.log('second_then_1');
  await console.log('second_then_2');
}


async function opExperiment() {
  await first()
  await second()

  await Promise.resolve(console.log('third_resolve'))
    .then(() => console.log('third_then_1'))
    .then(() => console.log('third_then_2'))
  await Promise.resolve(console.log('fourth_resolve'))
    .then(() => console.log('fourth_then_1'))
    .then(() => console.log('fourth_then_2'))

  console.log('end');
}

opExperiment()

【讨论】:

  • 那么在事件循环方面对原始行为的解释是什么?
  • @WhatEver - 我不确定在代码中没有明确说明异步执行顺序的情况下(甚至跨执行)可以肯定地说什么。像你一样启动函数大致就像 Promise.all() 一样。这不能保证传递的承诺有一个特定的开始(当然也不是结束)顺序
  • 所以 thens 本身没有特定的顺序?为什么second_then_1 每次都比first_then_2 解决得更快,因为它们都立即解决?
  • 另外,看看你观察到的序列,我觉得它是明智的:first() 开始执行,开始 first_resolve。发生这种情况时,调用了 second()(在 first_resolve 超时排队之后,因为我们没有先等待),到 first_resolve 记录时, second_resolve 已准备好记录,并且在发生这种情况时, first_then_1 已完成,并且很快。它是所有时间的日志记录。看起来很直观,这些会以您观察到的方式交错(尽管再次依赖这种行为是错误的)
  • 所以我想这将取决于承诺解决所需的时间。谢谢。
猜你喜欢
  • 1970-01-01
  • 2019-06-27
  • 1970-01-01
  • 1970-01-01
  • 2015-11-25
  • 2015-06-02
  • 2011-04-14
  • 1970-01-01
  • 2017-02-23
相关资源
最近更新 更多