Skip to content

Instantly share code, notes, and snippets.

@nivrith
Last active May 28, 2022 09:59
Show Gist options
  • Save nivrith/3e93b11d12dbd338151ff0e39c599b07 to your computer and use it in GitHub Desktop.
Save nivrith/3e93b11d12dbd338151ff0e39c599b07 to your computer and use it in GitHub Desktop.
Powerful trick to batch promises using Async Generators
/**
* 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