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) => void
function asyncQueue<TValue>(fn, initialOptions): (item, position, runOnItemsChange) => void

Defined in: async-queuer.ts:612

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

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 & object

position

QueuePosition = ...

runOnItemsChange

boolean = true

Returns

void

Example

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