🌳
pt0/sharedF/moDashF/promF/batchPromCallsAI.mts
1// Like allPromCalls but limits concurrency to batchSize (default 10)
2// If shouldStop returns true for any result, stops processing remaining batches
3export const batchPromCalls = async <T, R>(
4 data: T[],
5 fnc: (item: T) => Promise<R>,
6 batchSize = 10,
7 shouldStop?: (result: R) => boolean
8): Promise<R[]> => {
9 const results: R[] = []
10 for (let i = 0; i < data.length; i += batchSize) {
11 const batch = data.slice(i, i + batchSize)
12 const batchResults = await Promise.all(batch.map(fnc))
13 results.push(...batchResults)
14 if (shouldStop && batchResults.some(shouldStop)) break
15 }
16 return results