Last active
June 25, 2018 19:29
-
-
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
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
// 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