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

queue

Function: queue()

ts
function queue<TValue>(fn, options): (item, position, runOnUpdate) => boolean
function queue<TValue>(fn, options): (item, position, runOnUpdate) => boolean

Defined in: queuer.ts:549

Creates a queue that processes items immediately upon addition. Items are processed sequentially in FIFO order by default.

This is a simplified wrapper around the Queuer class that only exposes the addItem method. The queue is always running and will process items as they are added. For more control over queue processing, use the Queuer class directly.

Example usage:

ts
// Basic sequential processing
const processItems = queue<number>((n) => console.log(n), {
  wait: 1000,
  onItemsChange: (queuer) => console.log(queuer.peekAllItems())
});
processItems(1); // Logs: 1
processItems(2); // Logs: 2 after 1 completes

// Priority queue
const processPriority = queue<number>((n) => console.log(n), {
  getPriority: n => n // Higher numbers processed first
});
processPriority(1);
processPriority(3); // Processed before 1
// Basic sequential processing
const processItems = queue<number>((n) => console.log(n), {
  wait: 1000,
  onItemsChange: (queuer) => console.log(queuer.peekAllItems())
});
processItems(1); // Logs: 1
processItems(2); // Logs: 2 after 1 completes

// Priority queue
const processPriority = queue<number>((n) => console.log(n), {
  getPriority: n => n // Higher numbers processed first
});
processPriority(1);
processPriority(3); // Processed before 1

Type Parameters

TValue

Parameters

fn

(item) => void

options

QueuerOptions<TValue>

Returns

Function

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.

Returns true if the item was added, false if the queue is full.

Example usage:

ts
queuer.addItem('task');
queuer.addItem('task2', 'front');
queuer.addItem('task');
queuer.addItem('task2', 'front');

Parameters

item

TValue

position

QueuePosition = ...

runOnUpdate

boolean = true

Returns

boolean

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.