Framework
Version
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference
Batcher API Reference

useAsyncBatcher

Function: useAsyncBatcher()

ts
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:

  • Handles promises and returns results from batch executions
  • Provides error handling with configurable error behavior
  • Tracks success, error, and settle counts separately
  • Has state tracking for when batches are executing
  • Returns the result of the batch function execution

Features:

  • Configurable batch size and wait time
  • Custom batch processing logic via getShouldExecute
  • Event callbacks for monitoring batch operations
  • Error handling for failed batch operations
  • Automatic or manual batch processing

The batcher collects items and processes them in batches based on:

  • Maximum batch size (number of items per batch)
  • Time-based batching (process after X milliseconds)
  • Custom batch processing logic via getShouldExecute

Error Handling:

  • If an onError handler is provided, it will be called with the error and batcher instance
  • If throwOnError is true (default when no onError handler is provided), the error will be thrown
  • If throwOnError is false (default when onError handler is provided), the error will be swallowed
  • Both onError and throwOnError can be used together - the handler will be called before any error is thrown
  • The error state can be checked using the underlying AsyncBatcher instance

Type Parameters

TValue

TSelected = AsyncBatcherState<TValue>

Parameters

fn

(items) => Promise<any>

options

AsyncBatcherOptions<TValue> = {}

selector?

(state) => TSelected

Returns

ReactAsyncBatcher<TValue, TSelected>

Example

tsx
// 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();
Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.