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

AsyncQueuer

Class: AsyncQueuer<TValue>

Defined in: async-queuer.ts:157

A flexible asynchronous queue for processing tasks with configurable concurrency, priority, and expiration.

Features:

  • Priority queue support via the getPriority option
  • Configurable concurrency limit
  • Callbacks for task success, error, completion, and queue state changes
  • FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
  • Pause and resume processing
  • Task cancellation
  • Item expiration to remove stale items from the queue

Tasks are processed concurrently up to the configured concurrency limit. When a task completes, the next pending task is processed if the concurrency limit allows.

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 AsyncQueuer instance

Example usage:

ts
const asyncQueuer = new AsyncQueuer<string>(async (item) => {
  // process item
  return item.toUpperCase();
}, {
  concurrency: 2,
  onSuccess: (result) => {
    console.log(result);
  }
});

asyncQueuer.addItem('hello');
asyncQueuer.start();
const asyncQueuer = new AsyncQueuer<string>(async (item) => {
  // process item
  return item.toUpperCase();
}, {
  concurrency: 2,
  onSuccess: (result) => {
    console.log(result);
  }
});

asyncQueuer.addItem('hello');
asyncQueuer.start();

Type Parameters

• TValue

Constructors

new AsyncQueuer()

ts
new AsyncQueuer<TValue>(fn, initialOptions): AsyncQueuer<TValue>
new AsyncQueuer<TValue>(fn, initialOptions): AsyncQueuer<TValue>

Defined in: async-queuer.ts:171

Parameters

fn

(value) => Promise<any>

initialOptions

AsyncQueuerOptions<TValue>

Returns

AsyncQueuer<TValue>

Methods

addItem()

ts
addItem(
   item, 
   position, 
   runOnItemsChange): void
addItem(
   item, 
   position, 
   runOnItemsChange): void

Defined in: async-queuer.ts:312

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');

clear()

ts
clear(): void
clear(): void

Defined in: async-queuer.ts:282

Removes all pending items from the queue. Does not affect active tasks.

Returns

void


execute()

ts
execute(position?): Promise<any>
execute(position?): Promise<any>

Defined in: async-queuer.ts:410

Removes and returns the next item from the queue and executes the task function with it.

Parameters

position?

QueuePosition

Returns

Promise<any>

Example

ts
queuer.execute();
// LIFO
queuer.execute('back');
queuer.execute();
// LIFO
queuer.execute('back');

getConcurrency()

ts
getConcurrency(): number
getConcurrency(): number

Defined in: async-queuer.ts:215

Returns the current concurrency limit for processing items. If a function is provided, it is called with the queuer instance.

Returns

number


getErrorCount()

ts
getErrorCount(): number
getErrorCount(): number

Defined in: async-queuer.ts:552

Returns the number of items that have failed processing.

Returns

number


getExpirationCount()

ts
getExpirationCount(): number
getExpirationCount(): number

Defined in: async-queuer.ts:587

Returns the number of items that have expired and been removed from the queue.

Returns

number


getIsEmpty()

ts
getIsEmpty(): boolean
getIsEmpty(): boolean

Defined in: async-queuer.ts:503

Returns true if the queue is empty (no pending items).

Returns

boolean


getIsFull()

ts
getIsFull(): boolean
getIsFull(): boolean

Defined in: async-queuer.ts:510

Returns true if the queue is full (reached maxSize).

Returns

boolean


getIsIdle()

ts
getIsIdle(): boolean
getIsIdle(): boolean

Defined in: async-queuer.ts:580

Returns true if the queuer is running but has no items to process and no active tasks.

Returns

boolean


getIsRunning()

ts
getIsRunning(): boolean
getIsRunning(): boolean

Defined in: async-queuer.ts:573

Returns true if the queuer is currently running (processing items).

Returns

boolean


getNextItem()

ts
getNextItem(position): undefined | TValue
getNextItem(position): undefined | TValue

Defined in: async-queuer.ts:380

Removes and returns the next item from the queue without executing the task function. Use for manual queue management. Normally, use execute() to process items.

Parameters

position

QueuePosition = ...

Returns

undefined | TValue

Example

ts
// FIFO
queuer.getNextItem();
// LIFO
queuer.getNextItem('back');
// FIFO
queuer.getNextItem();
// LIFO
queuer.getNextItem('back');

getOptions()

ts
getOptions(): AsyncQueuerOptions<TValue>
getOptions(): AsyncQueuerOptions<TValue>

Defined in: async-queuer.ts:199

Returns the current queuer options, including defaults and any overrides.

Returns

AsyncQueuerOptions<TValue>


getRejectionCount()

ts
getRejectionCount(): number
getRejectionCount(): number

Defined in: async-queuer.ts:566

Returns the number of items that have been rejected from being added to the queue.

Returns

number


getSettledCount()

ts
getSettledCount(): number
getSettledCount(): number

Defined in: async-queuer.ts:559

Returns the number of items that have completed processing (success or error).

Returns

number


getSize()

ts
getSize(): number
getSize(): number

Defined in: async-queuer.ts:517

Returns the number of pending items in the queue.

Returns

number


getSuccessCount()

ts
getSuccessCount(): number
getSuccessCount(): number

Defined in: async-queuer.ts:545

Returns the number of items that have been successfully processed.

Returns

number


getWait()

ts
getWait(): number
getWait(): number

Defined in: async-queuer.ts:207

Returns the current wait time (in milliseconds) between processing items. If a function is provided, it is called with the queuer instance.

Returns

number


peekActiveItems()

ts
peekActiveItems(): TValue[]
peekActiveItems(): TValue[]

Defined in: async-queuer.ts:531

Returns the items currently being processed (active tasks).

Returns

TValue[]


peekAllItems()

ts
peekAllItems(): TValue[]
peekAllItems(): TValue[]

Defined in: async-queuer.ts:524

Returns a copy of all items in the queue, including active and pending items.

Returns

TValue[]


peekNextItem()

ts
peekNextItem(position): undefined | TValue
peekNextItem(position): undefined | TValue

Defined in: async-queuer.ts:493

Returns the next item in the queue without removing it.

Parameters

position

QueuePosition = 'front'

Returns

undefined | TValue

Example

ts
queuer.peekNextItem(); // front
queuer.peekNextItem('back'); // back
queuer.peekNextItem(); // front
queuer.peekNextItem('back'); // back

peekPendingItems()

ts
peekPendingItems(): TValue[]
peekPendingItems(): TValue[]

Defined in: async-queuer.ts:538

Returns the items waiting to be processed (pending tasks).

Returns

TValue[]


reset()

ts
reset(withInitialItems?): void
reset(withInitialItems?): void

Defined in: async-queuer.ts:291

Resets the queuer to its initial state. Optionally repopulates with initial items. Does not affect callbacks or options.

Parameters

withInitialItems?

boolean

Returns

void


setOptions()

ts
setOptions(newOptions): void
setOptions(newOptions): void

Defined in: async-queuer.ts:192

Updates the queuer options. New options are merged with existing options.

Parameters

newOptions

Partial<AsyncQueuerOptions<TValue>>

Returns

void


start()

ts
start(): void
start(): void

Defined in: async-queuer.ts:261

Starts processing items in the queue. If already running, does nothing.

Returns

void


stop()

ts
stop(): void
stop(): void

Defined in: async-queuer.ts:273

Stops processing items in the queue. Does not clear the queue.

Returns

void

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.