我有一个包含 promise 数组的数组,每个内部数组可以有 4k、2k 或 500 个 promise 。
总共有大约 60k 个 promise ,我也可以使用其他值对其进行测试。
现在我需要执行 Promise.all(BigArray[0])
。
完成第一个内部数组后,我需要执行下一个 Promise.all(BigArray[1])
等等。
如果我尝试执行 Promise.all(BigArray)
它的抛出:
fatal error call_and_retry_2 allocation failed - process out of memory
我需要按顺序执行每个 promise,而不是并行执行,我认为这就是 Node 正在做的事情。 我不应该使用新的库,但我愿意考虑答案!
编辑:
下面是一段代码示例:
function getInfoForEveryInnerArgument(InnerArray) {
const CPTPromises = _.map(InnerArray, (argument) => getDBInfo(argument));
return Promise.all(CPTPromises)
.then((results) => {
return doSomethingWithResults(results);
});
}
function mainFunction() {
BigArray = [[argument1, argument2, argument3, argument4], [argument5, argument6, argument7, argument8], ....];
//the summ of all arguments is over 60k...
const promiseArrayCombination = _.map(BigArray, (InnerArray, key) => getInfoForEveryInnerArgument(InnerArray));
Promise.all(promiseArrayCombination).then((fullResults) => {
console.log(fullResults);
return fullResults;
})
}
最佳答案
Promise.all
将不起作用,您可以使用 Array.reduce
来逐一处理 BigArray
元素:
BigArray.reduce((promiseChain, currentArray) => {
return promiseChain.then(chainResults =>
Promise.all(currentArray).then(currentResult =>
[...chainResults, currentResult]
)
);
}, Promise.resolve([])).then(arrayOfArraysOfResults => {
// Do something with all results
});
关于javascript - 依次执行 Promise.all,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37196699/