Framework
Version
防抖器 API 參考
節流器 API 參考
速率限制器 API 參考
佇列 API 參考

AsyncQueuer

Class: AsyncQueuer<TValue>

Defined in: async-queuer.ts:123

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.

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

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

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

Removes all items from the queuer

Returns

void


getActiveItems()

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

Defined in: async-queuer.ts:462

Returns the active items

Returns

() => Promise<TValue>[]


getAllItems()

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

Defined in: async-queuer.ts:455

Returns a copy of all items in the queuer

Returns

() => Promise<TValue>[]


getExecutionCount()

ts
getExecutionCount(): number
getExecutionCount(): number

Defined in: async-queuer.ts:476

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

Returns

number


getExpirationCount()

ts
getExpirationCount(): number
getExpirationCount(): number

Defined in: async-queuer.ts:538

Returns the number of items that have expired from the queuer

Returns

number


getIsEmpty()

ts
getIsEmpty(): boolean
getIsEmpty(): boolean

Defined in: async-queuer.ts:434

Returns true if the queuer is empty

Returns

boolean


getIsFull()

ts
getIsFull(): boolean
getIsFull(): boolean

Defined in: async-queuer.ts:441

Returns true if the queuer is full

Returns

boolean


getIsIdle()

ts
getIsIdle(): boolean
getIsIdle(): boolean

Defined in: async-queuer.ts:497

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

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

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

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

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

Returns the pending items

Returns

() => Promise<TValue>[]


getRejectionCount()

ts
getRejectionCount(): number
getRejectionCount(): number

Defined in: async-queuer.ts:483

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

Returns the current size of the queuer

Returns

number


onError()

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

Defined in: async-queuer.ts:516

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

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

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

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

Updates the queuer options Returns the new options state

Parameters

newOptions

Partial<AsyncQueuerOptions<TValue>>

Returns

void


start()

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

Defined in: async-queuer.ts:272

Starts the queuer and processes items

Returns

Promise<void>


stop()

ts
stop(): void
stop(): void

Defined in: async-queuer.ts:295

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.