Skip to content

Instantly share code, notes, and snippets.

@ccnokes
Last active June 25, 2018 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccnokes/6415b07b8c47530e88218cc6d0a7e1c7 to your computer and use it in GitHub Desktop.
Save ccnokes/6415b07b8c47530e88218cc6d0a7e1c7 to your computer and use it in GitHub Desktop.
Chunk async tasks into batches and process in parallel v2. Demo: https://jsfiddle.net/ccnokes/jhu635r8
// see working demo here: https://jsfiddle.net/ccnokes/jhu635r8/73/
async function runTasks(taskFns /* Array<() => Promise<any>> */, concurrency = 3) {
let results = [];
taskFns = new Set(taskFns);
let pending = new Set();
for (let task of taskFns) {
if (pending.size >= concurrency) {
await Promise.race(pending);
}
let p = task().then((result) => {
pending.delete(p);
results.push(result);
return result;
});
pending.add(p);
taskFns.delete(task);
}
await Promise.all(pending);
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment