Manages a queue of asynchronous tasks, each potentially with an abort mechanism. The queue supports concurrency control and optional delay between task executions.

const queue = new Queue({ concurrency: 2, delay: 100 });

const controller = new AbortController();
const fetchData = () => fetch('https://api.example.com/data', { signal: controller.signal });
queue.add(fetchData, controller.abort);

setTimeout(() => {
queue.abort();
}, 200);

This queue class is designed to help manage asynchronous tasks that might need to be aborted. It is ideal for scenarios where tasks are initiated based on external events and might need to be cancelled if conditions change.

Constructors

Methods

Constructors

  • Parameters

    • options: PromiseQueueOptions = {}

      Configuration options for the queue.

    Returns default

Methods

  • Aborts all tasks in the queue that have an abort function.

    Returns void

    queue.abort(); // Aborts all abortable tasks in the queue.
    
  • Adds a task to the queue, which can be either a simple asynchronous function or an array containing a function and its abort handler.

    Parameters

    • promise: PromiseFuncOrPromiseWithAbortArray

      The asynchronous task function or a tuple containing the task and an abort function.

    • Optionalabort: (() => void)

      An optional abort function for the task.

        • (): void
        • Returns void

    Returns void

    // Add a task without an abort function.
    queue.add(() => new Promise(resolve => setTimeout(resolve, 1000)));

    // Add a task with an abort function.
    const controller = new AbortController();
    queue.add(() => fetch('https://example.com', { signal: controller.signal }), controller.abort);