javascript - 为每个内部执行并行承诺
问题描述
我正在尝试在 forEach 中执行一组承诺 A、B、C、D。这些承诺应该对所有列表项按顺序并行执行。我希望执行等到所有承诺都得到解决。但是一旦遇到 forEach 中的第一个 Promise,流程就会开始并行处理其他函数。在下面的代码片段中,列表中的所有 promise 都被解析后,应该触发 thirdCall。但是,它仍然不起作用
async function getList(value) {
return someList;
}
async function processList(list) {
try {
await list.forEach(async (element) => {
await A(element.firstProperty); // console jumps to thirdCall() while executing A,B,C,D in parallel
await B(element.secondProperty);
await C(element.thirdProperty);
await D(element.fourthProperty);
});
return response;
} catch (e) {
throw (e);
}
}
async function thirdCall() {
try {
let someresult = someProcessing(); // this should start execution ONLY AFTER processList() is done.
await E(someresult);
thirdCallResponse;
} catch (error) {
console.log(`error deleting policy ${error}`);
throw error;
}
}
async function processValue(value) {
try {
const list = await getList(value);
await processList(list);
await thirdCall(value);
} catch (error) {
console.log(error);
}
}
processValue('someGuid');
解决方案
推荐阅读
- python - 两个日期列之间的 np.busday_count 给出 -1 作为每一行的结果
- c - C中大尺寸数组声明的问题
- python - 带有连接的 Groupby agg 不会产生预期的输出
- java - 在 Java 中创建数字网格
- groovy - 获取 Json 并将其转换为列表 [Groovy]
- docker - 用于 TensorFlow 服务的 Prometheus
- python - 如何检查键是否存在于Python中的值和键中的值中
- android - GUI在android工作室中消失了
- swift - 使用存储在两个数组中的数字和权重计算加权平均值
- javascript - 画布不着色