mirror of
https://github.com/allaunthefox/Research-Stack.git
synced 2026-07-31 03:05:21 +00:00
20 lines
608 B
JavaScript
20 lines
608 B
JavaScript
export async function mapWithConcurrency(items, concurrency, mapper) {
|
|
const list = Array.isArray(items) ? items : [];
|
|
const workerCount = Math.min(
|
|
list.length,
|
|
Math.max(1, Math.floor(Number(concurrency) || 1))
|
|
);
|
|
const results = new Array(list.length);
|
|
let nextIndex = 0;
|
|
|
|
async function runWorker() {
|
|
while (nextIndex < list.length) {
|
|
const currentIndex = nextIndex;
|
|
nextIndex += 1;
|
|
results[currentIndex] = await mapper(list[currentIndex], currentIndex);
|
|
}
|
|
}
|
|
|
|
await Promise.all(Array.from({ length: workerCount }, () => runWorker()));
|
|
return results;
|
|
}
|