Skip to content

batchProcess

batchProcess<T, R>(targets, callback, options): Promise<R[]>

Defined in: index.ts:116

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.

T = any

R = any

T[]

(prop) => Promise<R>

BatchProcessOptions & object

Promise<R[]>

Basic 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]]

With progress tracking and rate limiting

await batchProcess(
userIds,
async (id) => await fetchUser(id),
{
batchSize: 5,
delayBetweenBatches: 1000, // Wait 1 second between batches
onProgress: (completed, total) => {
console.log(`Progress: ${completed}/${total} batches`)
}
}
)

With retry and error handling

const results = await batchProcess(
items,
async (item) => await processItem(item),
{
retry: { maxAttempts: 3, backoff: 'exponential' },
continueOnError: true,
timeout: 5000
}
)

With flattened results

const result = await batchProcess(
[1, 2, 3, 4, 5, 6],
async (num) => num * 2,
{ batchSize: 2, flatten: true }
)
// Output: [2, 4, 6, 8, 10, 12]

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)
}

batchProcess<T, R>(targets, callback, options?): Promise<R[][]>

Defined in: index.ts:123

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.

T = any

R = any

T[]

(prop) => Promise<R>

BatchProcessOptions & object

Promise<R[][]>

Basic 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]]

With progress tracking and rate limiting

await batchProcess(
userIds,
async (id) => await fetchUser(id),
{
batchSize: 5,
delayBetweenBatches: 1000, // Wait 1 second between batches
onProgress: (completed, total) => {
console.log(`Progress: ${completed}/${total} batches`)
}
}
)

With retry and error handling

const results = await batchProcess(
items,
async (item) => await processItem(item),
{
retry: { maxAttempts: 3, backoff: 'exponential' },
continueOnError: true,
timeout: 5000
}
)

With flattened results

const result = await batchProcess(
[1, 2, 3, 4, 5, 6],
async (num) => num * 2,
{ batchSize: 2, flatten: true }
)
// Output: [2, 4, 6, 8, 10, 12]

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)
}