Files
live-hub-py/web/frontend/src/utils/concurrency.ts
T

18 lines
437 B
TypeScript

export async function runWithConcurrency<T>(
items: T[],
limit: number,
worker: (item: T) => Promise<void>,
) {
let nextIndex = 0;
const workerCount = Math.min(Math.max(1, limit), items.length);
await Promise.all(
Array.from({ length: workerCount }, async () => {
while (nextIndex < items.length) {
const index = nextIndex;
nextIndex += 1;
await worker(items[index]);
}
}),
);
}