function asyncBatch<TValue>(fn, options): (item) => void
function asyncBatch<TValue>(fn, options): (item) => void
Defined in: async-batcher.ts:469
Creates an async batcher that processes items in batches
Unlike the sync batcher, this async version:
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
void
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.