Defined in: async-queuer.ts:315
A flexible asynchronous queue for processing tasks with configurable concurrency, priority, and expiration.
Async vs Sync Versions: The async version provides advanced features over the sync Queuer:
The sync Queuer is lighter weight and simpler when you don't need async features, return values, or execution control.
What is Queuing? Queuing is a technique for managing and processing items sequentially or with controlled concurrency. Tasks are processed up to the configured concurrency limit. When a task completes, the next pending task is processed if the concurrency limit allows.
Key Features:
Error Handling:
State Management:
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:327
(item) => Promise<any>
AsyncQueuerOptions<TValue> = {}
AsyncQueuer<TValue>
asyncRetryers: Map<number, AsyncRetryer<(item) => Promise<any>>>;
asyncRetryers: Map<number, AsyncRetryer<(item) => Promise<any>>>;
Defined in: async-queuer.ts:321
fn: (item) => Promise<any>;
fn: (item) => Promise<any>;
Defined in: async-queuer.ts:328
TValue
Promise<any>
key: string;
key: string;
Defined in: async-queuer.ts:319
options: AsyncQueuerOptions<TValue>;
options: AsyncQueuerOptions<TValue>;
Defined in: async-queuer.ts:320
readonly store: Store<Readonly<AsyncQueuerState<TValue>>>;
readonly store: Store<Readonly<AsyncQueuerState<TValue>>>;
Defined in: async-queuer.ts:316
_emit(): void
_emit(): void
Defined in: async-queuer.ts:366
Emits a change event for the async queuer instance. Mostly useful for devtools.
void
abort(): void
abort(): void
Defined in: async-queuer.ts:836
Aborts all ongoing executions with the internal abort controllers. Does NOT clear out the items.
void
addItem(
item,
position,
runOnItemsChange): boolean
addItem(
item,
position,
runOnItemsChange): boolean
Defined in: async-queuer.ts:474
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
QueuePosition = ...
boolean = true
boolean
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:801
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:609
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');
flush(numberOfItems, position?): Promise<void>
flush(numberOfItems, position?): Promise<void>
Defined in: async-queuer.ts:657
Processes a specified number of items to execute immediately with no wait time If no numberOfItems is provided, all items will be processed
number = ...
Promise<void>
flushAsBatch(batchFunction): Promise<void>
flushAsBatch(batchFunction): Promise<void>
Defined in: async-queuer.ts:671
Processes all items in the queue as a batch using the provided function as an argument The queue is cleared after processing
(items) => Promise<any>
Promise<void>
getAbortSignal(executeCount?): null | AbortSignal
getAbortSignal(executeCount?): null | AbortSignal
Defined in: async-queuer.ts:826
Returns the AbortSignal for a specific execution. If no executeCount is provided, returns the signal for the most recent execution. Returns null if no execution is found or not currently executing.
number
Optional specific execution to get signal for
null | AbortSignal
const queuer = new AsyncQueuer(
async (item: string) => {
const signal = queuer.getAbortSignal()
if (signal) {
const response = await fetch(`/api/process/${item}`, { signal })
return response.json()
}
},
{ concurrency: 2 }
)
const queuer = new AsyncQueuer(
async (item: string) => {
const signal = queuer.getAbortSignal()
if (signal) {
const response = await fetch(`/api/process/${item}`, { signal })
return response.json()
}
},
{ concurrency: 2 }
)
getNextItem(position): undefined | TValue
getNextItem(position): undefined | TValue
Defined in: async-queuer.ts:557
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');
peekActiveItems(): TValue[]
peekActiveItems(): TValue[]
Defined in: async-queuer.ts:763
Returns the items currently being processed (active tasks).
TValue[]
peekAllItems(): TValue[]
peekAllItems(): TValue[]
Defined in: async-queuer.ts:756
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:746
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:770
Returns the items waiting to be processed (pending tasks).
TValue[]
reset(): void
reset(): void
Defined in: async-queuer.ts:847
Resets the queuer state to its default values
void
setOptions(newOptions): void
setOptions(newOptions): void
Defined in: async-queuer.ts:371
Updates the queuer options. New options are merged with existing options.
Partial<AsyncQueuerOptions<TValue>>
void
start(): void
start(): void
Defined in: async-queuer.ts:777
Starts processing items in the queue. If already running, does nothing.
void
stop(): void
stop(): void
Defined in: async-queuer.ts:787
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.
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.
