Defined in: async-queuer.ts:157
A flexible asynchronous queue for processing tasks with configurable concurrency, priority, and expiration.
Features:
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:
Example usage:
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();
• TValue
new AsyncQueuer<TValue>(fn, initialOptions): AsyncQueuer<TValue>
new AsyncQueuer<TValue>(fn, initialOptions): AsyncQueuer<TValue>
Defined in: async-queuer.ts:171
(value) => Promise<any>
AsyncQueuerOptions<TValue>
AsyncQueuer<TValue>
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.
TValue & object
QueuePosition = ...
boolean = true
void
queuer.addItem({ value: 'task', priority: 10 });
queuer.addItem('task2', 'front');
queuer.addItem({ value: 'task', priority: 10 });
queuer.addItem('task2', 'front');
clear(): void
clear(): void
Defined in: async-queuer.ts:282
Removes all pending items from the queue. Does not affect active tasks.
void
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.
Promise<any>
queuer.execute();
// LIFO
queuer.execute('back');
queuer.execute();
// LIFO
queuer.execute('back');
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.
number
getErrorCount(): number
getErrorCount(): number
Defined in: async-queuer.ts:552
Returns the number of items that have failed processing.
number
getExpirationCount(): number
getExpirationCount(): number
Defined in: async-queuer.ts:587
Returns the number of items that have expired and been removed from the queue.
number
getIsEmpty(): boolean
getIsEmpty(): boolean
Defined in: async-queuer.ts:503
Returns true if the queue is empty (no pending items).
boolean
getIsFull(): boolean
getIsFull(): boolean
Defined in: async-queuer.ts:510
Returns true if the queue is full (reached maxSize).
boolean
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.
boolean
getIsRunning(): boolean
getIsRunning(): boolean
Defined in: async-queuer.ts:573
Returns true if the queuer is currently running (processing items).
boolean
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.
QueuePosition = ...
undefined | TValue
// FIFO
queuer.getNextItem();
// LIFO
queuer.getNextItem('back');
// FIFO
queuer.getNextItem();
// LIFO
queuer.getNextItem('back');
getOptions(): AsyncQueuerOptions<TValue>
getOptions(): AsyncQueuerOptions<TValue>
Defined in: async-queuer.ts:199
Returns the current queuer options, including defaults and any overrides.
AsyncQueuerOptions<TValue>
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.
number
getSettledCount(): number
getSettledCount(): number
Defined in: async-queuer.ts:559
Returns the number of items that have completed processing (success or error).
number
getSize(): number
getSize(): number
Defined in: async-queuer.ts:517
Returns the number of pending items in the queue.
number
getSuccessCount(): number
getSuccessCount(): number
Defined in: async-queuer.ts:545
Returns the number of items that have been successfully processed.
number
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.
number
peekActiveItems(): TValue[]
peekActiveItems(): TValue[]
Defined in: async-queuer.ts:531
Returns the items currently being processed (active tasks).
TValue[]
peekAllItems(): TValue[]
peekAllItems(): TValue[]
Defined in: async-queuer.ts:524
Returns a copy of all items in the queue, including active and pending items.
TValue[]
peekNextItem(position): undefined | TValue
peekNextItem(position): undefined | TValue
Defined in: async-queuer.ts:493
Returns the next item in the queue without removing it.
QueuePosition = 'front'
undefined | TValue
queuer.peekNextItem(); // front
queuer.peekNextItem('back'); // back
queuer.peekNextItem(); // front
queuer.peekNextItem('back'); // back
peekPendingItems(): TValue[]
peekPendingItems(): TValue[]
Defined in: async-queuer.ts:538
Returns the items waiting to be processed (pending tasks).
TValue[]
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.
boolean
void
setOptions(newOptions): void
setOptions(newOptions): void
Defined in: async-queuer.ts:192
Updates the queuer options. New options are merged with existing options.
Partial<AsyncQueuerOptions<TValue>>
void
start(): void
start(): void
Defined in: async-queuer.ts:261
Starts processing items in the queue. If already running, does nothing.
void
stop(): void
stop(): void
Defined in: async-queuer.ts:273
Stops processing items in the queue. Does not clear the queue.
void
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.