Skip to content

Instantly share code, notes, and snippets.

@emeraldsanto
Created February 26, 2022 17:39
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emeraldsanto/a1b9cfad850c40b0115e95322c5887d4 to your computer and use it in GitHub Desktop.
Save emeraldsanto/a1b9cfad850c40b0115e95322c5887d4 to your computer and use it in GitHub Desktop.
Utility function to sequentially run batches of async operations with configurable concurrency.
/**
* Allows for sequentially running batches of async operations.
* @param {number} concurrency The maximum number of operations to run concurrently.
*/
export function batch(concurrency: number) {
return async function <TElement, TResult>(xs: Array<TElement>, handler: (x: TElement) => TResult): Promise<Array<TResult>> {
const results: Array<TResult> = [];
const batches = Array.from({ length: Math.ceil(xs.length / concurrency) }, (_, i) => i)
for await (const batchNumber of batches) {
const startingIndex = batchNumber * concurrency;
const elementsToBatch = xs.slice(startingIndex, startingIndex + concurrency);
const promises = elementsToBatch.map(async (v) => {
const res = await handler(v);
results.push(res);
});
await Promise.all(promises);
}
return results;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment