Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save batrudinych/5db293ab829f19475dd43c40e01a923e to your computer and use it in GitHub Desktop.
Save batrudinych/5db293ab829f19475dd43c40e01a923e to your computer and use it in GitHub Desktop.
Running async operations in parallel. Concurrency limit applied correctly
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