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

后端 未结 6 1351
猫巷女王i
猫巷女王i 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:53

    An answer from October 2020. Async/await makes it short: only 10 code lines+JSDoc.

    /**
     * Same as Promise.all(), but it waits for the first {batchSize} promises to finish
     * before starting the next batch.
     *
     * @template A
     * @template B
     * @param {function(A): B} task The task to run for each item.
     * @param {A[]} items Arguments to pass to the task for each call.
     * @param {int} batchSize
     * @returns {B[]}
     */
    async promiseAllInBatches(task, items, batchSize) {
        let position = 0;
        let results = [];
        while (position < items.length) {
            const itemsForBatch = items.slice(position, position + batchSize);
            results = [...results, ...await Promise.all(itemsForBatch.map(item => task(item)))];
            position += batchSize;
        }
        return results;
    }
    

提交回复
热议问题