Skip to content

Instantly share code, notes, and snippets.

@leaysgur
Created April 28, 2023 00:32
Show Gist options
  • Save leaysgur/598546298c5699e71782120ee35488fb to your computer and use it in GitHub Desktop.
Save leaysgur/598546298c5699e71782120ee35488fb to your computer and use it in GitHub Desktop.
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