Skip to content

Instantly share code, notes, and snippets.

@BrenoAlberto
Created March 6, 2023 22:52
Show Gist options
  • Save BrenoAlberto/18de61f4f992ffc596eb3ae072795f62 to your computer and use it in GitHub Desktop.
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…
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