← Back
async/await js

Promises in batches

Previously at Things we learn #

Parallel execution of promises:

await Promise.all(arrayOfPromises);

Sequential execution of promises:

for (const promise of arrayOfPromises) {
await promise;
}

Today: Promises in batches #

Combine sequential and parallel execution:

const _ = require("lodash");

async function runInBatches(arrayOfPromises, batchSize = 10) {
const batch = _.chunk(arrayOfPromises, batchSize);
let results = [];
for (const batch of batches) {
results = results.concat(await Promise.all(batch));
}
return results;
}

🖖