Created
September 28, 2017 19:53
-
-
Save evanlouie/73ab5ca435ec409d45c289a0ca58a195 to your computer and use it in GitHub Desktop.
Limiting Concurrrent Execution
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
async function concurrent(maxConcurrency = 4) { | |
const thingsToOperateOn = []; | |
const createConcurrentPromise = () => { | |
const thing = thingsToOperateOn.shift(); | |
return new Promise((resolve, reject) => { | |
// Do something to thing | |
console.log(thing); | |
}) | |
.then(() => { | |
createConcurrentPromise(); | |
}) | |
.catch(err => { | |
console.error(err); | |
}); | |
}; | |
// Spawn array of concurrent promises | |
Array(maxConcurrency) | |
.fill(null) | |
.map(() => createConcurrentPromise()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment