Files
firecrawl/apps/api/src/lib/batch-process.ts
T

16 lines
424 B
TypeScript
Raw Normal View History

2024-04-15 17:01:47 -04:00
export async function batchProcess<T>(
2024-12-11 19:46:11 -03:00
array: T[],
batchSize: number,
asyncFunction: (item: T, index: number) => Promise<void>
): Promise<void> {
const batches: T[][] = [];
for (let i = 0; i < array.length; i += batchSize) {
const batch = array.slice(i, i + batchSize);
batches.push(batch);
2024-04-15 17:01:47 -04:00
}
2024-12-11 19:46:11 -03:00
for (const batch of batches) {
await Promise.all(batch.map((item, i) => asyncFunction(item, i)));
}
}