javascript - 依次执行 Promise.all

标签 javascript node.js promise

我有一个包含 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/

相关文章:

javascript - 从 Angular Controller 中的 php 文件获取 JSONP 数据

javascript - Knockout.js utils.arrayFilter 属性无法正常工作

javascript - 如何使用 Node.js 从给定路径中选择多个随机文件?

linux - 在 jailed shell 上安装 node.js

node.js - 如何在node js中导入boxicons

javascript - Promise 同步函数

objective-c - PromiseKit 框架 - 对成员 'then()' 的模糊引用

javascript - 安装 npm 模块导致找不到命令

javascript - 用于移动设备的 HTML5 持久存储

javascript - JavaScript 中 `promise` 的实现