Execute batch of promises in series. Once Promise.all is done go to the next batch

后端 未结 6 1871
星月不相逢
星月不相逢 2021-02-19 15:57

I have an array that contains an array of promises, and each inner array could have either 4k, 2k or 500 promises.

In total there are around 60k promises and I may test

6条回答
  •  离开以前
    2021-02-19 16:41

    Your question is a bit misnamed which may have confused some folks in this question and in the previous version of this question. You are trying to execute a batch of async operations in series, one batch of operations, then when that is done execute another batch of operations. The results of those async operations are tracked with promises. Promises themselves represent async operations that have already been started. "Promises" aren't executed themselves. So technically, you don't "execute a batch of promises in series". You execute a set of operations, track their results with promises, then execute the next batch when the first batch is all done.

    Anyway, here's a solution to serializing each batch of operations.

    You can create an inner function which I usually call next() that lets you process each iteration. When the promise resolves from processing one innerArray, you call next() again:

    function mainFunction() {
        return new Promise(function(resolve, reject) {
            var bigArray = [[argument1, argument2, argument3, argument4], [argument5, argument6, argument7, argument8], ....];
            //the summ of all arguments is over 60k...
            var results = [];
    
            var index = 0;
            function next() {
                if (index < bigArray.length) {
                    getInfoForEveryInnerArgument(bigArray[index++]).then(function(data) {
                        results.push(data);
                        next();
                    }, reject);
                } else {
                    resolve(results);
                }
            }
            // start first iteration
            next();
        });
    }
    

    This also collects all the sub-results into a results array and returns a master promise who's resolved value is this results array. So, you could use this like:

    mainFunction().then(function(results) {
        // final results array here and everything done
    }, function(err) {
        // some error here
    });
    

    You could also use the .reduce() design pattern for iterating an array serially:

    function mainFunction() {
        var bigArray = [[argument1, argument2, argument3, argument4], [argument5, argument6, argument7, argument8], ....];
        return bigArray.reduce(function(p, item) {
            return p.then(function(results) {
                return getInfoForEveryInnerArgument(item).then(function(data) {
                    results.push(data);
                    return results;
                })
            });
        }, Promise.resolve([]));
    }
    

    This creates more simultaneous promises than the first option and I don't know if that is an issue for such a large set of promises (which is why I offered the original option), but this code is cleaner and the concept is convenient to use for other situations too.


    FYI, there are some promise add-on features built for doing this for you. In the Bluebird promise library (which is a great library for development using promises), they have Promise.map() which is made for this:

    function mainFunction() {
        var bigArray = [[argument1, argument2, argument3, argument4], [argument5, argument6, argument7, argument8], ....];
        return Promise.map(bigArray, getInfoForEveryInnerArgument);
    
    }
    

提交回复
热议问题