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

useAsyncQueuerState

Function: useAsyncQueuerState()

ts
function useAsyncQueuerState<TValue>(options): [() => Promise<TValue>[], AsyncQueuer<TValue>]
function useAsyncQueuerState<TValue>(options): [() => Promise<TValue>[], AsyncQueuer<TValue>]

Defined in: async-queuer/useAsyncQueuerState.ts:53

A higher-level React hook that creates an AsyncQueuer instance with built-in state management.

This hook combines an AsyncQueuer with React state to automatically track the queue items. It returns a tuple containing:

  • The current array of queued items as React state
  • The queuer instance with methods to control the queue

The queue can be configured with:

  • Maximum concurrent operations
  • Maximum queue size
  • Processing function for queue items
  • Various lifecycle callbacks

The state will automatically update whenever items are:

  • Added to the queue
  • Removed from the queue
  • Started processing
  • Completed processing

Type Parameters

TValue

Parameters

options

AsyncQueuerOptions<TValue> = {}

Returns

[() => Promise<TValue>[], AsyncQueuer<TValue>]

Example

tsx
// Create a queue with state management
const [queueItems, asyncQueuer] = useAsyncQueuerState({
  concurrency: 2,
  maxSize: 100,
  started: true
});

// Add items to queue - state updates automatically
asyncQueuer.addItem(async () => {
  const result = await fetchData();
  return result;
});

// Start processing
asyncQueuer.start();

// Stop processing
asyncQueuer.stop();

// queueItems reflects current queue state
const pendingCount = asyncQueuer.getPendingItems().length;
// Create a queue with state management
const [queueItems, asyncQueuer] = useAsyncQueuerState({
  concurrency: 2,
  maxSize: 100,
  started: true
});

// Add items to queue - state updates automatically
asyncQueuer.addItem(async () => {
  const result = await fetchData();
  return result;
});

// Start processing
asyncQueuer.start();

// Stop processing
asyncQueuer.stop();

// queueItems reflects current queue state
const pendingCount = asyncQueuer.getPendingItems().length;
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.