Skip to content

Instantly share code, notes, and snippets.

@carlhannes
Last active July 12, 2022 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlhannes/88c5b1e3faec316f5835f489ab2286fe to your computer and use it in GitHub Desktop.
Save carlhannes/88c5b1e3faec316f5835f489ab2286fe to your computer and use it in GitHub Desktop.
Batch promises in javascript
/**
* Same as Promise.all(items.map(item => task(item))), but it waits for
* the first {batchSize} promises to finish before starting the next batch.
*
* Usage:
* const results = await batchPromise(STUFF_TO_DO, 5, async (ITEM_TO_DO) => {
* console.log('Fetching', ITEM_TO_DO.url);
* return await fetch(ITEM_TO_DO.url);
* });
*
* @template A
* @template B
* @param {A[]} items Arguments to pass to the task for each call.
* @param {int} batchSize
* @param {function(A): B} task The task to run for each item.
* @returns {B[]}
*/
export default async function batchPromise(items, batchSize, task) {
let position = 0;
let results = [];
while (position < items.length) {
const itemsForBatch = items.slice(position, position + batchSize);
results = [
...results,
...await Promise.all(
// eslint-disable-next-line no-loop-func
itemsForBatch.map((item, index) => task(item, position + index)),
)];
position += batchSize;
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment