this repo has no description
1export async function batchAsync<T, R>(items: T[], task: (item: T) => Promise<R>, concurrency = 5) {
2 const results: R[] = new Array(items.length);
3
4 let index = 0;
5
6 async function worker() {
7 while (index < items.length) {
8 const currentIndex = index++;
9 try {
10 results[currentIndex] = await task(items[currentIndex]);
11 } catch (error) {
12 console.error(`Error processing item at index ${currentIndex}:`, error);
13 results[currentIndex] = null;
14 }
15 }
16 }
17
18 const workers = Array.from({ length: Math.min(concurrency, items.length) }, () => worker());
19
20 await Promise.all(workers);
21
22 return results;
23}