repomix
Version:
A tool to pack repository contents to single file for AI consumption
22 lines (21 loc) • 761 B
JavaScript
export const mapWithConcurrency = async (items, concurrency, fn) => {
if (!Number.isInteger(concurrency) || concurrency < 1) {
throw new Error(`concurrency must be a positive integer (received ${concurrency})`);
}
if (items.length === 0) {
return [];
}
const results = Array.from({ length: items.length });
let nextIndex = 0;
const worker = async () => {
while (true) {
const index = nextIndex++;
if (index >= items.length)
return;
results[index] = await fn(items[index], index);
}
};
const workerCount = Math.min(concurrency, items.length);
await Promise.all(Array.from({ length: workerCount }, () => worker()));
return results;
};