function asyncBatch<TValue>(fn, options): (item) => Promise<any>
function asyncBatch<TValue>(fn, options): (item) => Promise<any>
Defined in: async-batcher.ts:587
Creates an async batcher that processes items in batches.
Async vs Sync Versions: The async version provides advanced features over the sync batch function:
The sync batch function is lighter weight and simpler when you don't need async features, return values, or execution control.
What is Batching? Batching is a technique for grouping multiple operations together to be processed as a single unit.
Configuration Options:
Error Handling:
State Management:
• TValue
(items) => Promise<any>
AsyncBatcherOptions<TValue>
Function
Adds an item to the async batcher If the batch size is reached, timeout occurs, or shouldProcess returns true, the batch will be processed
TValue
Promise<any>
The result from the batch function, or undefined if an error occurred and was handled by onError
The error from the batch function if no onError handler is configured or throwOnError is true
const batchItems = asyncBatch<number>(
async (items) => {
const result = await processApiCall(items);
console.log('Processing:', items);
return result;
},
{
maxSize: 3,
wait: 1000,
onSuccess: (result) => console.log('Batch succeeded:', result),
onError: (error) => console.error('Batch failed:', error)
}
);
batchItems(1);
batchItems(2);
batchItems(3); // Triggers batch processing
const batchItems = asyncBatch<number>(
async (items) => {
const result = await processApiCall(items);
console.log('Processing:', items);
return result;
},
{
maxSize: 3,
wait: 1000,
onSuccess: (result) => console.log('Batch succeeded:', result),
onError: (error) => console.error('Batch failed:', error)
}
);
batchItems(1);
batchItems(2);
batchItems(3); // Triggers batch processing
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.
