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