Skip to content

Instantly share code, notes, and snippets.

@evanlouie
Created September 28, 2017 19:53
Show Gist options
  • Save evanlouie/73ab5ca435ec409d45c289a0ca58a195 to your computer and use it in GitHub Desktop.
Save evanlouie/73ab5ca435ec409d45c289a0ca58a195 to your computer and use it in GitHub Desktop.
Limiting Concurrrent Execution
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