Created
March 6, 2023 22:52
-
-
Save BrenoAlberto/18de61f4f992ffc596eb3ae072795f62 to your computer and use it in GitHub Desktop.
It takes an array of functions that return promises (asyncFunctions) and the maximum number of functions to be executed concurrently (maxConcurrent). The function will execute up to maxConcurrent functions in parallel, and once a function completes, it will start a new one until all functions have completed. The function returns a promise that r…
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
export async function concurrentTaskQueue<T>(asyncFunctions: Array<() => Promise<T>>, maxConcurrent: number): Promise<T[]> { | |
let currentIndex = 0; | |
const runningFunctions = new Set<Promise<T>>(); | |
const orderedRunnedFunctions: Map<Promise<T>, number> = new Map(); | |
let results: Array<T> = Array(asyncFunctions.length); | |
while (currentIndex < asyncFunctions.length) { | |
while (runningFunctions.size < maxConcurrent && currentIndex < asyncFunctions.length) { | |
const asyncFunction = asyncFunctions[currentIndex]; | |
const promise = asyncFunction().finally(() => { | |
runningFunctions.delete(promise); | |
}); | |
runningFunctions.add(promise); | |
orderedRunnedFunctions.set(promise, currentIndex); | |
currentIndex++; | |
} | |
await Promise.race(Array.from(runningFunctions)) | |
} | |
await Promise.all(Array.from(runningFunctions)); | |
for (const [promise, index] of orderedRunnedFunctions) { | |
results[index] = await promise; | |
} | |
return results | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment