Results and progress
Result order
Section titled “Result order”Results keep the order of the input array, regardless of which task inside a batch finishes first.
Nested or flat results
Section titled “Nested or flat results”By default, results are grouped by batch:
await batchProcess([1, 2, 3, 4, 5, 6], async (num) => num * 2, { batchSize: 2 })// [[2, 4], [6, 8], [10, 12]]With flatten: true you get a single array. The return type changes to Array<R> through a function overload:
await batchProcess([1, 2, 3, 4, 5, 6], async (num) => num * 2, { batchSize: 2, flatten: true})// [2, 4, 6, 8, 10, 12]Progress tracking
Section titled “Progress tracking”onProgress(completed, total) is called after each batch completes. Both values count batches, not items.
await batchProcess( userIds, async (userId) => await fetchUserData(userId), { batchSize: 10, onProgress: (completed, total) => { const percentage = ((completed / total) * 100).toFixed(1) console.log(`Progress: ${completed}/${total} batches (${percentage}%)`) } })Combining options
Section titled “Combining options”const results = await batchProcess<Task, Result | Error>( tasks, async (task) => await processTask(task), { batchSize: 5, delayBetweenBatches: 1000, retry: { maxAttempts: 3, backoff: 'exponential' }, continueOnError: true, timeout: 10000, flatten: true, onProgress: (completed, total) => { console.log(`Processing: ${completed}/${total} batches`) } })