A minimalist async task queue/serializer for JavaScript.
TaskQueue ensures async operations execute in FIFO (first-in, first-out) order, preventing race conditions when multiple async tasks need to be serialized.
Use directly as an ES module via jsdelivr:
import { TaskQueue } from 'https://cdn.jsdelivr.net/gh/mesgjs/task-queue/src/task-queue.esm.js';import { TaskQueue } from 'https://cdn.jsdelivr.net/gh/mesgjs/task-queue/src/task-queue.esm.js';
const queue = new TaskQueue();
// Queue async tasks
const result1 = await queue.add(async () => {
// Your async operation
return 'result';
});
const result2 = await queue.add(async () => {
// This runs after result1 completes
return 'another result';
});Creates a new task queue instance.
Queues an async function for execution.
- Parameters:
callback- Async function to execute - Returns: Promise that resolves with the callback's return value
- Throws: Rejects if the queue is shutting down
Cancels a queued task. By default, promises reject with Cancelled.
- Parameters:
callback- The callback function to cancelresolve- The promise will resolve to this if notundefinedreject- The promise will reject with this if notundefined
- Returns:
trueif cancelled,falseif not found
Shuts down the queue and rejects all pending operations with Shutting down.
Returns the number of pending operations in the queue.
See LICENSE file for details.
Copyright 2025-2026 Kappa Computer Solutions, LLC and Brian Katzung