Last active
May 28, 2022 09:59
-
-
Save nivrith/3e93b11d12dbd338151ff0e39c599b07 to your computer and use it in GitHub Desktop.
Powerful trick to batch promises using Async Generators
This file contains hidden or 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
/** | |
* A fuction that returns a Promise | |
* @typedef {(<T = any>() => Promise<T>)} Task | |
*/ | |
/** | |
* | |
* @param {Array<Task>} tasks | |
*/ | |
export async function* batchTasks(tasks, limit, taskCallback = (r) => r) { | |
// iterate over tasks | |
for (let i = 0; i < tasks.length; i = i + limit) { | |
// grab the batch of tasks for current iteration | |
const batch = tasks.slice(i, i + limit); | |
// wait for them to resolve concurrently | |
const result = await Promise.all( | |
// optionally attach callback to perform any side effects | |
batch.map((task) => task().then((r) => taskCallback(r))) | |
); | |
// yield the batched result and let consumer know | |
yield result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment