batchProcess
このコンテンツはまだ日本語訳がありません。
Call Signature
Section titled “Call Signature”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.
Type Parameters
Section titled “Type Parameters”T = any
R = any
Parameters
Section titled “Parameters”targets
Section titled “targets”T[]
callback
Section titled “callback”(prop) => Promise<R>
options
Section titled “options”BatchProcessOptions & object
Returns
Section titled “Returns”Promise<R[]>
Examples
Section titled “Examples”Basic usage
// Process 10 items in batches of 3const 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)}Call Signature
Section titled “Call Signature”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.
Type Parameters
Section titled “Type Parameters”T = any
R = any
Parameters
Section titled “Parameters”targets
Section titled “targets”T[]
callback
Section titled “callback”(prop) => Promise<R>
options?
Section titled “options?”BatchProcessOptions & object
Returns
Section titled “Returns”Promise<R[][]>
Examples
Section titled “Examples”Basic usage
// Process 10 items in batches of 3const 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)}