function useAsyncBatcher<TValue, TSelected>(
fn,
options,
selector?): ReactAsyncBatcher<TValue, TSelected>
function useAsyncBatcher<TValue, TSelected>(
fn,
options,
selector?): ReactAsyncBatcher<TValue, TSelected>
Defined in: react-pacer/src/async-batcher/useAsyncBatcher.ts:77
A React hook that creates an AsyncBatcher instance for managing asynchronous batches of items.
This is the async version of the useBatcher hook. Unlike the sync version, this async batcher:
Features:
The batcher collects items and processes them in batches based on:
Error Handling:
• TValue
• TSelected = AsyncBatcherState<TValue>
(items) => Promise<any>
AsyncBatcherOptions<TValue> = {}
(state) => TSelected
ReactAsyncBatcher<TValue, TSelected>
// Basic async batcher for API requests
const asyncBatcher = useAsyncBatcher(
async (items) => {
const results = await Promise.all(items.map(item => processItem(item)));
return results;
},
{
maxSize: 10,
wait: 2000,
onSuccess: (result) => {
console.log('Batch processed successfully:', result);
},
onError: (error) => {
console.error('Batch processing failed:', error);
}
}
);
// Add items to batch
asyncBatcher.addItem(newItem);
// Manually execute batch
const result = await asyncBatcher.execute();
// Basic async batcher for API requests
const asyncBatcher = useAsyncBatcher(
async (items) => {
const results = await Promise.all(items.map(item => processItem(item)));
return results;
},
{
maxSize: 10,
wait: 2000,
onSuccess: (result) => {
console.log('Batch processed successfully:', result);
},
onError: (error) => {
console.error('Batch processing failed:', error);
}
}
);
// Add items to batch
asyncBatcher.addItem(newItem);
// Manually execute batch
const result = await asyncBatcher.execute();
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.