Registry / workflow / js-queue

js-queue

JSON →
library2.0.2jsnpmunverified

js-queue provides a straightforward JavaScript queue implementation for both Node.js and browser environments, featuring an 'auto-run' capability where the queue processes new items as they are added, provided it's not explicitly stopped. The current stable version is 2.0.2, with releases appearing to be made as needed for updates and fixes, rather than a strict cadence. A key differentiator is its simplicity and explicit support for both CommonJS environments (Node.js, Webpack, Browserify) and vanilla browser usage. Additionally, since version 2.0.0, it exposes an ES6-based Last In First Out (LIFO) stack via `js-queue/stack.js`, offering an alternative data structure with a similar interface, useful for different application needs.

npm install js-queue
INSTALL
IMPORT
SIG · JS-QUEUE
J
js-queue
workflowjavascriptv2.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Queue
const Queue = require('js-queue');
import Queue from 'js-queue';
The primary import pattern for the Queue class uses CommonJS `require`. While ES6 modules are supported for the internal `easy-stack` utility, the main `js-queue` entry point is designed for CommonJS or environments that transpile it.
Stack
const Stack = require('js-queue/stack.js');
import { Stack } from 'js-queue/stack.js';
The LIFO stack implementation, based on `easy-stack`, is exposed via a direct path. It uses ES6 classes and thus requires Node 6+ or a compatible environment. It is also CommonJS `require` based.
queue instance
const queue = new Queue();
const queue = Queue();
The `Queue` is a class and must be instantiated using the `new` keyword to create a functional queue instance.

This example demonstrates how to create a queue, add functions to it, and ensure the queue progresses using `this.next()` within the queued functions, showcasing the auto-run behavior.

const Queue = require('js-queue'); // Create a new queue instance const queue = new Queue(); let requestCount = 0; function makeRequest() { requestCount++; console.log(`Making request #${requestCount}. Queue length: ${queue.contents.length - 1}`); // Simulate an async operation setTimeout(() => { // Critical: Call this.next() to process the next item in the queue if (this && typeof this.next === 'function') { this.next(); } else { console.error('Context not bound or next() missing. Queue might stall.'); } }, 100); } // Add a bunch of items (functions) to the queue for (let i = 0; i < 10; i++) { queue.add(makeRequest); } // Demonstrating manual addition and auto-run setTimeout(() => { console.log('\nAdding more requests after a delay...'); queue.add(makeRequest, makeRequest, makeRequest); }, 1500);
Debug
Known issues
breakingVersion 2.0.0 introduced the exposure of the `easy-stack` module via `require('js-queue/stack.js')`. While not a direct breaking change to the core queue API, it signifies a shift in architecture and introduces a dependency (easy-stack) that internally uses ES6 classes, requiring Node 6+ for that specific module. If you relied on older Node versions and planned to use the stack, this could break.
fix
Ensure your Node.js environment is version 6 or higher if you intend to use the `easy-stack` functionality via `js-queue/stack.js`. For the core queue, compatibility remains broad.
affects: >=2.0.0
gotchaForgetting to call `this.next()` within the function executed by the queue will cause the queue to stall. The `next` method must be explicitly invoked by the queued function (typically at the end of its asynchronous or synchronous operation) to signal completion and allow the queue to process the subsequent item.
fix
Always include `this.next();` at the point your queued function has completed its work. If using an asynchronous operation, ensure `this.next()` is called after the async task resolves (e.g., in a callback or promise `.then()`).
affects: >=0.0.1
breakingThe license for `js-queue` changed from 'Do What The F*ck You Want To Public License' (DBAD) to MIT License in version 2.0.1. While not a code-level breaking change, it alters the legal terms of use, distribution, and modification, which could be significant for some projects or organizations.
fix
Review the MIT License terms to ensure compliance with your project's legal requirements. If you require the previous DBAD license, you must explicitly use version 2.0.0 or earlier.
affects: >=2.0.1
Errors
Common errors & fixes
TypeError: Queue is not a constructor
Attempting to call `Queue` directly without `new`, or incorrect CommonJS `require` syntax in an ES module context.
fix
Instantiate the queue correctly using `const queue = new Queue();`. If in an ES module context, you might need to reconsider `js-queue` or use a bundler to handle CommonJS `require`.
Queue stalls, functions are added but never execute subsequent items.
The functions added to the queue are not calling `this.next()` upon completion.
fix
Ensure that every function you add to the queue explicitly calls `this.next()` once its operation is finished, particularly for asynchronous tasks.
ReferenceError: require is not defined or SyntaxError: Cannot use import statement outside a module
Attempting to use `require()` in an ES module environment without a bundler, or `import` in a CommonJS environment without Babel/TypeScript.
fix
If in Node.js, ensure your file is treated as CommonJS (default `.js` or `type: 'commonjs'` in `package.json`). If in a browser, use the provided vanilla JS script tag method or a bundler like Webpack/Parcel/Rollup configured for CommonJS.
Upgrade
Version history
2.0.2latest on npm
Audit
Dependencies
easy-stackrequiredInternal dependency for providing ES6-based LIFO stack functionality, exposed via 'js-queue/stack.js'.
Agent activity
30 hits · last 30 days
node
26
OpenAI (training)
1
Resources
js-queue — npm install js-queue · libregistry