javascript - 如何等待 Promise.all 中的 2 个 Promise 完成然后执行另一个任务
问题描述
我目前正在开发一个程序,我需要使用 Promise.all 同时运行几个功能。但是在我可以继续执行任务之前,我只需要完成 2 个承诺然后运行 .then(),我该怎么做呢?
例子:
await Promise.all([Task1(),Task2(),Task3(),Task4(),Task5()]);
当仅(例如)Task1 和 Task 4 完成时,我需要它来继续代码。
我试图通过使用 while 循环等待 Task1 和 Task2 通过在完成时设置变量完成来进行试验,但是那样。根本不起作用。
解决方案
在评论中,您似乎说过您事先特别知道哪两个比其余的更紧急(在本例中,它是 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);
});
推荐阅读
- rust - 如何在 Rust 中缺少 Copy 的借用元素向量上计算算术运算?
- google-analytics - 如何在谷歌分析中获取帖子网址
- kotlin - Ktor 提示来自基本身份验证块内的路由拦截器内的凭据
- reactjs - 受控的单选按钮警告使用 formik 字段处理它的值
- scala - Spark的LogisticRegression在哪里做数据标准化?
- javascript - 如何根据不同的登录在 Web 应用程序中创建多个视图?
- node.js - 使用以前下载的源代码重新创建 HTTP 触发器
- python - 加速和优化 Python 循环
- c++ - QLabel 没有正确更新
- r - ggplot2 中带有 geom_bar() 的回归线