Skip to content

Instantly share code, notes, and snippets.

@batrudinych
Created January 14, 2019 10:58
Show Gist options
  • Save batrudinych/11de3cb5efa5eea8c908d85b5e331a65 to your computer and use it in GitHub Desktop.
Save batrudinych/11de3cb5efa5eea8c908d85b5e331a65 to your computer and use it in GitHub Desktop.
Running async operations in parallel and harvesting the results. Concurrency limit applied incorrectly
async function take3subtake0() {
const concurrencyLimit = 5;
let results = [];
const batchesCount = Math.ceil(numberOfOperations / concurrencyLimit);
// Running Promises in parallel in batches
for (let i = 0; i < batchesCount; i++) {
const batchStart = i * concurrencyLimit;
const batchArguments = listOfArguments.slice(batchStart, batchStart + concurrencyLimit);
const batchPromises = batchArguments.map(asyncOperation);
// Harvesting
const batchResults = await Promise.all(batchPromises);
results = results.concat(batchResults);
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment