Class

ConcurrentCallbackQueue

ConcurrentCallbackQueue(options)

A queue implementation that allows for concurrent execution of asynchronous operations.

This class provides a way to schedule and manage a queue of callback functions that will be executed concurrently, up to a specified limit. When the maximum number of concurrent callbacks is reached, the queue will wait until one of the running callbacks finishes before starting a new one. The queue can be configured to start automatically when a callback is added, and it can also be stopped and restarted as needed.

A main use case is to manage asynchronous operations that need to be executed in parallel, such as making multiple API requests or processing a large number of files.

Constructor

# new ConcurrentCallbackQueue(options)

Creates a new concurrent callback queue.

Creates a new concurrent callback queue.

Parameters:
Name Type Description
options QueueOptions

Queue configuration options.

Version:
  • 0.8.32
Since:
  • 2023-03-23
Author:
  • David Urbina (davidurbina.dev@gmail.com)
Tutorials:
See:

View Source ConcurrentCallbackQueue.js, line 146

Examples
Basic usage
const queue = new ConcurrentCallbackQueue();
queue.enqueue(() => fetch('https://httpstat.us/200,400?sleep=2000'));
Custom options
const queue = new ConcurrentCallbackQueue({
   autoStart: false,
   onCallbackError: (error) => console.error(error),
});
queue.enqueue(() => throw new Error('Error'));
queue.start();
Multiple callbacks
const queue = new ConcurrentCallbackQueue();
queue.enqueueAll([
  () => fetch('https://httpstat.us/200,400?sleep=2000'),
  () => fetch('https://httpstat.us/200,400?sleep=2000'),
]);

Classes

create
Creates a new concurrent callback queue.

Methods

# clear() → {Array.<CallbackTuple>}

Stops the execution of the queue and removes all pending callbacks from it.

Stops the execution of the queue and removes all pending callbacks from it.

View Source ConcurrentCallbackQueue.js, line 617

List of pending callbacks

Array.<CallbackTuple>

# dequeue() → {CallbackTuple|undefined}

Removes a pending callback from the queue without stopping execution.

Removes a pending callback from the queue without stopping execution.

View Source ConcurrentCallbackQueue.js, line 559

Removed callback tuple or undefined if the queue is empty.

CallbackTuple | undefined

# dequeueAll() → {Array.<CallbackTuple>}

Removes all pending callbacks from the queue without stopping the queue execution.

Removes all pending callbacks from the queue without stopping the queue execution.

View Source ConcurrentCallbackQueue.js, line 569

List of pending callbacks

Array.<CallbackTuple>

# enqueue(callback, retriesopt) → {void}

Adds a callback to the queue, if autoStart is enabled the queue execution starts.

Adds a callback to the queue, if autoStart is enabled the queue execution starts. You can specify an optional number of retries in case of an error.

Parameters:
Name Type Attributes Default Description
callback function

The callback function to add to the queue.

retries number <optional>
0

Number of retry attempts in case of an error (optional).

View Source ConcurrentCallbackQueue.js, line 502

If the callback is not a function or retries is not a number.

Error
void

# enqueueAll(callbacks, retriesopt) → {void}

Adds multiple callbacks to the queue, if autoStart is enabled the queue execution starts.

Adds multiple callbacks to the queue, if autoStart is enabled the queue execution starts. You can specify an optional number of retry attempts in case of an error.

Parameters:
Name Type Attributes Default Description
callbacks Array.<function()>

The array of callback functions to add to the queue.

retries number <optional>
0

Number of retry attempts in case of an error for all callbacks (optional).

View Source ConcurrentCallbackQueue.js, line 530

If callbacks is not an array of functions or retries is not a number.

Error
void

# getOptions()

Returns the current queue configuration options.

Returns the current queue configuration options.

View Source ConcurrentCallbackQueue.js, line 484

# getPendingCount() → {number}

Returns the number of pending callbacks in the queue.

Returns the number of pending callbacks in the queue.

View Source ConcurrentCallbackQueue.js, line 467

number

# getRunningCount() → {number}

Returns the number of running callbacks in the queue.

Returns the number of running callbacks in the queue.

View Source ConcurrentCallbackQueue.js, line 477

number

# getState() → {string}

Returns the current state of the queue.

Returns the current state of the queue.

View Source ConcurrentCallbackQueue.js, line 457

string

# start() → {void}

Starts the execution of the queue.

Starts the execution of the queue.

If the queue is stopped at any point and then restarted, the execution resumes from the last pending callback.

View Source ConcurrentCallbackQueue.js, line 587

void

# stop() → {void}

Stops the execution of the queue, but does not remove pending callbacks.

Stops the execution of the queue, but does not remove pending callbacks.

Calling this method will not stop the execution of callbacks that are already being processed, nor will it remove pending callbacks from the queue, so if the queue is restarted, it will resume from the last pending callback. Sets the queue state to STOPPED.

View Source ConcurrentCallbackQueue.js, line 607

void