Created
February 26, 2022 17:39
-
-
Save emeraldsanto/a1b9cfad850c40b0115e95322c5887d4 to your computer and use it in GitHub Desktop.
Utility function to sequentially run batches of async operations with configurable concurrency.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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