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
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;
}