Process items in batches, running items within each batch in parallel, but processing batches sequentially.

This is useful for rate limiting API calls or controlling resource usage.

// Process 10 items in batches of 3
const result = await batchProcess(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
async (num) => {
await someAsyncOperation(num)
return num * 2
},
{ batchSize: 3 }
)
// Output: [[2, 4, 6], [8, 10, 12], [14, 16, 18], [20]]

For simple sequential processing without batching, use native for...of:

const results = []
for (const item of items) {
const result = await processItem(item)
results.push(result)
}
  • Type Parameters

    • T = any
    • R = any

    Parameters

    • targets: T[]
    • callback: (prop: T) => Promise<R>
    • Optionaloptions: { batchSize?: number }

    Returns Promise<R[][]>