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

asyncQueue

Function: asyncQueue()

ts
function asyncQueue<TValue>(fn, initialOptions): (item, position, runOnItemsChange) => boolean
function asyncQueue<TValue>(fn, initialOptions): (item, position, runOnItemsChange) => boolean

Defined in: async-queuer.ts:733

Creates a new AsyncQueuer instance and returns a bound addItem function for adding tasks. The queuer is started automatically and ready to process items.

Error Handling:

  • If an onError handler is provided, it will be called with the error and queuer 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 AsyncQueuer instance

State Management:

  • Uses TanStack Store for reactive state management
  • Use initialState to provide initial state values when creating the async queuer
  • Use onSuccess callback to react to successful task execution and implement custom logic
  • Use onError callback to react to task execution errors and implement custom error handling
  • Use onSettled callback to react to task execution completion (success or error) and implement custom logic
  • Use onItemsChange callback to react to items being added or removed from the queue
  • Use onExpire callback to react to items expiring and implement custom logic
  • Use onReject callback to react to items being rejected when the queue is full
  • The state includes error count, expiration count, rejection count, running status, and success/settle counts
  • State can be accessed via the underlying AsyncQueuer instance's store.state property
  • When using framework adapters (React/Solid), state is accessed from the hook's state property

Example usage:

ts
const enqueue = asyncQueue<string>(async (item) => {
  return item.toUpperCase();
}, {...options});

enqueue('hello');
const enqueue = asyncQueue<string>(async (item) => {
  return item.toUpperCase();
}, {...options});

enqueue('hello');

Type Parameters

TValue

Parameters

fn

(value) => Promise<any>

initialOptions

AsyncQueuerOptions<TValue>

Returns

Function

Adds an item to the queue. If the queue is full, the item is rejected and onReject is called. Items can be inserted based on priority or at the front/back depending on configuration.

Parameters

item

TValue

position

QueuePosition = ...

runOnItemsChange

boolean = true

Returns

boolean

Example

ts
queuer.addItem({ value: 'task', priority: 10 });
queuer.addItem('task2', 'front');
queuer.addItem({ value: 'task', priority: 10 });
queuer.addItem('task2', 'front');
Our Partners
Unkey
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.

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.