首页 > 解决方案 > 如何等待 Promise.all 中的 2 个 Promise 完成然后执行另一个任务

问题描述

我目前正在开发一个程序,我需要使用 Promise.all 同时运行几个功能。但是在我可以继续执行任务之前,我只需要完成 2 个承诺然后运行 ​​.then(),我该怎么做呢?

例子:

await Promise.all([Task1(),Task2(),Task3(),Task4(),Task5()]);

当仅(例如)Task1 和 Task 4 完成时,我需要它来继续代码。

我试图通过使用 while 循环等待 Task1 和 Task2 通过在完成时设置变量完成来进行试验,但是那样。根本不起作用。

标签: javascriptnode.jspromiseasync-await

解决方案


在评论中,您似乎说过您事先特别知道哪两个比其余的更紧急(在本例中,它是 Task1 和 Task4)。

然后只需使用Promise.all两次:

const allResults = Promise.all([
    Promise.all([Task1(), Task4()])
    .then(([result1, result4]) => {
        // Those two are done, do what you like with `result1` and `result4`...
        return [result1, result4];
    }),
    Task2(),
    Task3(),
    Task5()
])
.then(([[result1, result4], result2, result3, result5]) => {
    // All five are done now, let's put them in order
    return [result1, result2, result3, result4, result5];
})
.then(/*...*/)
.catch(/*...*/);

then在那里,我通过重新映射整个处理程序中的顺序来保留外链中的整体 1、2、3、4、5 顺序。


最初,我假设您想等到任何两个完成,而不是特定的两个。没有内置的,但它很容易编写:

function enough(promises, min) {
  if (typeof min !== "number") {
    return Promise.all(promises);
  }
  let counter = 0;
  const results = [];
  return new Promise((resolve, reject) => {
    let index = 0;
    for (const promise of promises) {
      let position = index++;
      promise.then(
        result => {
          results[position] = result;
          if (++counter >= min) {
            resolve(results);
          }
        },
        reject
      );
    }
  });
}

现场示例:

function enough(promises, min) {
  if (typeof min !== "number") {
    return Promise.all(promises);
  }
  let counter = 0;
  const results = [];
  return new Promise((resolve, reject) => {
    let index = 0;
    for (const promise of promises) {
      let position = index++;
      promise.then(
        result => {
          results[position] = result;
          if (++counter >= min) {
            resolve(results);
          }
        },
        reject
      );
    }
  });
}

const delay = (ms, ...args) => new Promise(resolve => setTimeout(resolve, ms, ...args));
const rnd = () => Math.random() * 1000;

enough(
  [
    delay(rnd(), "a"),
    delay(rnd(), "b"),
    delay(rnd(), "c"),
    delay(rnd(), "d"),
    delay(rnd(), "e")
  ],
  2
)
.then(results => {
  console.log(results);
})
.catch(error => {
  console.error(error);
});


推荐阅读