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); |