Created
January 14, 2019 10:58
-
-
Save batrudinych/5db293ab829f19475dd43c40e01a923e to your computer and use it in GitHub Desktop.
Running async operations in parallel. Concurrency limit applied correctly
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
async function take3subtake1part0() { | |
const concurrencyLimit = 5; | |
const argsCopy = listOfArguments.slice(); | |
const promises = new Array(concurrencyLimit).fill(Promise.resolve()); | |
// Recursively chain the next Promise to the currently executed Promise | |
function chainNext(p) { | |
if (argsCopy.length) { | |
const arg = argsCopy.shift(); | |
return p.then(() => { | |
const operationPromise = asyncOperation(arg); | |
return chainNext(operationPromise); | |
}) | |
} | |
return p; | |
} | |
await Promise.all(promises.map(chainNext)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment