Files
hyperframes/packages/cli/src/utils/concurrency.ts
T
Miguel Ángel 95a686c23d fix(cli): catalog loads registry items a few at a time so a slow link no longer times them out (#5829)
* fix(cli): catalog loads registry items a few at a time so a slow link no longer times them out

* chore(cli): keep the moved concurrency helper's doc to one line

* chore(cli): wrap the concurrency helper's doc under the line limit
2026-10-18 08:36:30 +00:00

22 lines
756 B
TypeScript

/** Runs `worker` over `inputs` with at most `limit` in flight. Results keep input order, and the
* first rejection rejects the run. */
export async function runWithConcurrencyLimit<T, R>(
inputs: readonly T[],
limit: number,
worker: (input: T, index: number) => Promise<R>,
): Promise<R[]> {
if (limit < 1) throw new Error(`runWithConcurrencyLimit: limit must be ≥ 1, got ${limit}`);
const results = new Array<R>(inputs.length);
let cursor = 0;
const workerCount = Math.min(limit, inputs.length);
await Promise.all(
Array.from({ length: workerCount }, async () => {
while (cursor < inputs.length) {
const idx = cursor++;
results[idx] = await worker(inputs[idx]!, idx);
}
}),
);
return results;
}