Created
April 28, 2023 00:32
-
-
Save leaysgur/598546298c5699e71782120ee35488fb to your computer and use it in GitHub Desktop.
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 const promiseAllWithConcurrency = async <T>( | |
queue: (() => Promise<T>)[], | |
concurrency: number = 1 | |
) => { | |
let index = 0; | |
const results: T[] = []; | |
const execThread = async () => { | |
while (index < queue.length) { | |
const curIndex = index++; | |
results[curIndex] = await queue[curIndex](); | |
} | |
}; | |
const threads = []; | |
for (let thread = 0; thread < concurrency; thread++) | |
threads.push(execThread()); | |
await Promise.all(threads); | |
return results; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment