Created
January 14, 2019 10:58
-
-
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
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 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