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

AsyncQueuer

Class: AsyncQueuer<TFn>

Defined in: async-queuer.ts:164

A flexible asynchronous queue that processes tasks with configurable concurrency control.

Features:

  • Priority queue support via getPriority option
  • Configurable concurrency limit
  • Task success/error/completion callbacks
  • FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
  • Pause/resume task processing
  • Task cancellation
  • Item expiration to clear 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 below the concurrency limit.

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

ts
const asyncQueuer = new AsyncQueuer<string>({
  concurrency: 2,
  onSuccess: (result) => {
    console.log(result); // 'Hello'
  }
});

asyncQueuer.addItem(async () => {
  return 'Hello';
});

asyncQueuer.start();
const asyncQueuer = new AsyncQueuer<string>({
  concurrency: 2,
  onSuccess: (result) => {
    console.log(result); // 'Hello'
  }
});

asyncQueuer.addItem(async () => {
  return 'Hello';
});

asyncQueuer.start();

Type Parameters

• TFn extends AsyncQueuerFn

Constructors

new AsyncQueuer()

ts
new AsyncQueuer<TFn>(initialOptions): AsyncQueuer<TFn>
new AsyncQueuer<TFn>(initialOptions): AsyncQueuer<TFn>

Defined in: async-queuer.ts:177

Parameters

initialOptions

AsyncQueuerOptions<TFn> = defaultOptions

Returns

AsyncQueuer<TFn>

Methods

addItem()

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

Defined in: async-queuer.ts:385

Adds a task to the queuer

Parameters

fn

TFn

position

QueuePosition = ...

runOnItemsChange

boolean = true

Returns

void


clear()

ts
clear(): void
clear(): void

Defined in: async-queuer.ts:363

Removes all items from the queuer

Returns

void


getActiveItems()

ts
getActiveItems(): TFn[]
getActiveItems(): TFn[]

Defined in: async-queuer.ts:505

Returns the active items

Returns

TFn[]


getAllItems()

ts
getAllItems(): TFn[]
getAllItems(): TFn[]

Defined in: async-queuer.ts:498

Returns a copy of all items in the queuer

Returns

TFn[]


getConcurrency()

ts
getConcurrency(): number
getConcurrency(): number

Defined in: async-queuer.ts:217

Returns the current concurrency limit

Returns

number


getErrorCount()

ts
getErrorCount(): number
getErrorCount(): number

Defined in: async-queuer.ts:526

Returns the number of items that have failed processing

Returns

number


getExpirationCount()

ts
getExpirationCount(): number
getExpirationCount(): number

Defined in: async-queuer.ts:561

Returns the number of items that have expired from the queuer

Returns

number


getIsEmpty()

ts
getIsEmpty(): boolean
getIsEmpty(): boolean

Defined in: async-queuer.ts:477

Returns true if the queuer is empty

Returns

boolean


getIsFull()

ts
getIsFull(): boolean
getIsFull(): boolean

Defined in: async-queuer.ts:484

Returns true if the queuer is full

Returns

boolean


getIsIdle()

ts
getIsIdle(): boolean
getIsIdle(): boolean

Defined in: async-queuer.ts:554

Returns true if the queuer is running but has no items to process

Returns

boolean


getIsRunning()

ts
getIsRunning(): boolean
getIsRunning(): boolean

Defined in: async-queuer.ts:547

Returns true if the queuer is running

Returns

boolean


getNextItem()

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

Defined in: async-queuer.ts:444

Removes and returns an item from the queuer

Parameters

position

QueuePosition = ...

Returns

undefined | TFn


getOptions()

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

Defined in: async-queuer.ts:203

Returns the current queuer options

Returns

AsyncQueuerOptions<TFn>


getPeek()

ts
getPeek(position): undefined | TFn
getPeek(position): undefined | TFn

Defined in: async-queuer.ts:467

Returns an item without removing it

Parameters

position

QueuePosition = 'front'

Returns

undefined | TFn


getPendingItems()

ts
getPendingItems(): TFn[]
getPendingItems(): TFn[]

Defined in: async-queuer.ts:512

Returns the pending items

Returns

TFn[]


getRejectionCount()

ts
getRejectionCount(): number
getRejectionCount(): number

Defined in: async-queuer.ts:540

Returns the number of items that have been rejected from the queuer

Returns

number


getSettledCount()

ts
getSettledCount(): number
getSettledCount(): number

Defined in: async-queuer.ts:533

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

Returns the current size of the queuer

Returns

number


getSuccessCount()

ts
getSuccessCount(): number
getSuccessCount(): number

Defined in: async-queuer.ts:519

Returns the number of items that have been successfully processed

Returns

number


getWait()

ts
getWait(): number
getWait(): number

Defined in: async-queuer.ts:210

Returns the current wait time between processing items

Returns

number


reset()

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

Defined in: async-queuer.ts:371

Resets the queuer to its initial state

Parameters

withInitialItems?

boolean

Returns

void


setOptions()

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

Defined in: async-queuer.ts:196

Updates the queuer options Returns the new options state

Parameters

newOptions

Partial<AsyncQueuerOptions<TFn>>

Returns

void


start()

ts
start(): Promise<void>
start(): Promise<void>

Defined in: async-queuer.ts:331

Starts the queuer and processes items

Returns

Promise<void>


stop()

ts
stop(): void
stop(): void

Defined in: async-queuer.ts:354

Stops the queuer from processing items

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.