Last active
July 12, 2022 06:18
-
-
Save carlhannes/88c5b1e3faec316f5835f489ab2286fe to your computer and use it in GitHub Desktop.
Batch promises in javascript
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
/** | |
* 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