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

AsyncQueuer

Class: AsyncQueuer<TValue>

Defined in: async-queuer.ts:105

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

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.

Example

ts
const asyncQueuer = new AsyncQueuer<string>({ concurrency: 2 });

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

asyncQueuer.start();

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

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

asyncQueuer.start();

asyncQueuer.onSuccess((result) => {
  console.log(result); // 'Hello'
});

Type Parameters

• TValue

Constructors

new AsyncQueuer()

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

Defined in: async-queuer.ts:117

Parameters

initialOptions

AsyncQueuerOptions<TValue> = defaultOptions

Returns

AsyncQueuer<TValue>

Methods

addItem()

ts
addItem(
   fn, 
   position, 
runOnUpdate): Promise<TValue>
addItem(
   fn, 
   position, 
runOnUpdate): Promise<TValue>

Defined in: async-queuer.ts:254

Adds a task to the queuer

Parameters

fn

() => Promise<TValue> & object

position

QueuePosition = ...

runOnUpdate

boolean = true

Returns

Promise<TValue>


clear()

ts
clear(): void
clear(): void

Defined in: async-queuer.ts:234

Removes all items from the queuer

Returns

void


getActiveItems()

ts
getActiveItems(): () => Promise<TValue>[]
getActiveItems(): () => Promise<TValue>[]

Defined in: async-queuer.ts:386

Returns the active items

Returns

() => Promise<TValue>[]


getAllItems()

ts
getAllItems(): () => Promise<TValue>[]
getAllItems(): () => Promise<TValue>[]

Defined in: async-queuer.ts:379

Returns a copy of all items in the queuer

Returns

() => Promise<TValue>[]


getExecutionCount()

ts
getExecutionCount(): number
getExecutionCount(): number

Defined in: async-queuer.ts:400

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

Returns

number


getIsEmpty()

ts
getIsEmpty(): boolean
getIsEmpty(): boolean

Defined in: async-queuer.ts:358

Returns true if the queuer is empty

Returns

boolean


getIsFull()

ts
getIsFull(): boolean
getIsFull(): boolean

Defined in: async-queuer.ts:365

Returns true if the queuer is full

Returns

boolean


getIsIdle()

ts
getIsIdle(): boolean
getIsIdle(): boolean

Defined in: async-queuer.ts:421

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

Returns true if the queuer is running

Returns

boolean


getNextItem()

ts
getNextItem(position): undefined | () => Promise<TValue>
getNextItem(position): undefined | () => Promise<TValue>

Defined in: async-queuer.ts:324

Removes and returns an item from the queuer

Parameters

position

QueuePosition = ...

Returns

undefined | () => Promise<TValue>


getOptions()

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

Defined in: async-queuer.ts:142

Returns the current queuer options

Returns

Required<AsyncQueuerOptions<TValue>>


getPeek()

ts
getPeek(position): undefined | () => Promise<TValue>
getPeek(position): undefined | () => Promise<TValue>

Defined in: async-queuer.ts:346

Returns an item without removing it

Parameters

position

QueuePosition = 'front'

Returns

undefined | () => Promise<TValue>


getPendingItems()

ts
getPendingItems(): () => Promise<TValue>[]
getPendingItems(): () => Promise<TValue>[]

Defined in: async-queuer.ts:393

Returns the pending items

Returns

() => Promise<TValue>[]


getRejectionCount()

ts
getRejectionCount(): number
getRejectionCount(): number

Defined in: async-queuer.ts:407

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

Returns

number


getSize()

ts
getSize(): number
getSize(): number

Defined in: async-queuer.ts:372

Returns the current size of the queuer

Returns

number


onError()

ts
onError(cb): () => void
onError(cb): () => void

Defined in: async-queuer.ts:440

Adds a callback to be called when a task errors

Parameters

cb

(error) => void

Returns

Function

Returns

void


onSettled()

ts
onSettled(cb): () => void
onSettled(cb): () => void

Defined in: async-queuer.ts:450

Adds a callback to be called when a task is settled

Parameters

cb

(result) => void

Returns

Function

Returns

void


onSuccess()

ts
onSuccess(cb): () => void
onSuccess(cb): () => void

Defined in: async-queuer.ts:428

Adds a callback to be called when a task succeeds

Parameters

cb

(result) => void

Returns

Function

Returns

void


reset()

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

Defined in: async-queuer.ts:242

Resets the queuer to its initial state

Parameters

withInitialItems?

boolean

Returns

void


setOptions()

ts
setOptions(newOptions): AsyncQueuerOptions<TValue>
setOptions(newOptions): AsyncQueuerOptions<TValue>

Defined in: async-queuer.ts:132

Updates the queuer options Returns the new options state

Parameters

newOptions

Partial<AsyncQueuerOptions<TValue>>

Returns

AsyncQueuerOptions<TValue>


start()

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

Defined in: async-queuer.ts:202

Starts the queuer and processes items

Returns

Promise<void>


stop()

ts
stop(): void
stop(): void

Defined in: async-queuer.ts:225

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.