Skip to content

Rate limiting

batchSize is the number of tasks that run at the same time. The next batch starts only after every task in the current batch has settled.

// At most 5 concurrent API calls
await batchProcess(userIds, async (userId) => {
return await fetchUserDataFromAPI(userId)
}, { batchSize: 5 })

The same option is useful for keeping memory and CPU usage under control:

// Process 10 items at a time to avoid memory issues
await batchProcess(largeDataset, async (data) => {
return await processHeavyOperation(data)
}, { batchSize: 10 })

delayBetweenBatches waits the given number of milliseconds before each batch except the first one.

// 5 requests at a time, 1 second pause between batches
await batchProcess(
apiRequests,
async (request) => await fetch(request.url),
{
batchSize: 5,
delayBetweenBatches: 1000
}
)

If you only need to split an array, use arrayBatch directly:

import { arrayBatch } from 'chunk-process'
arrayBatch([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3)
// [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

arrayBatch throws if batchSize is not a positive integer.