{"id":13394,"library":"js-queue","title":"Simple JavaScript Queue","description":"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.","status":"active","version":"2.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/RIAEvangelist/js-queue","tags":["javascript","queue","node","js","auto","run","execute","browser","react"],"install":[{"cmd":"npm install js-queue","lang":"bash","label":"npm"},{"cmd":"yarn add js-queue","lang":"bash","label":"yarn"},{"cmd":"pnpm add js-queue","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Internal dependency for providing ES6-based LIFO stack functionality, exposed via 'js-queue/stack.js'.","package":"easy-stack","optional":false}],"imports":[{"note":"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.","wrong":"import Queue from 'js-queue';","symbol":"Queue","correct":"const Queue = require('js-queue');"},{"note":"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.","wrong":"import { Stack } from 'js-queue/stack.js';","symbol":"Stack","correct":"const Stack = require('js-queue/stack.js');"},{"note":"The `Queue` is a class and must be instantiated using the `new` keyword to create a functional queue instance.","wrong":"const queue = Queue();","symbol":"queue instance","correct":"const queue = new Queue();"}],"quickstart":{"code":"const Queue = require('js-queue');\n\n// Create a new queue instance\nconst queue = new Queue();\n\nlet requestCount = 0;\n\nfunction makeRequest() {\n  requestCount++;\n  console.log(`Making request #${requestCount}. Queue length: ${queue.contents.length - 1}`);\n\n  // Simulate an async operation\n  setTimeout(() => {\n    // Critical: Call this.next() to process the next item in the queue\n    if (this && typeof this.next === 'function') {\n      this.next();\n    } else {\n        console.error('Context not bound or next() missing. Queue might stall.');\n    }\n  }, 100);\n}\n\n// Add a bunch of items (functions) to the queue\nfor (let i = 0; i < 10; i++) {\n  queue.add(makeRequest);\n}\n\n// Demonstrating manual addition and auto-run\nsetTimeout(() => {\n  console.log('\\nAdding more requests after a delay...');\n  queue.add(makeRequest, makeRequest, makeRequest);\n}, 1500);","lang":"javascript","description":"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."},"warnings":[{"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.","message":"Version 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.","severity":"breaking","affected_versions":">=2.0.0"},{"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()`).","message":"Forgetting 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.","severity":"gotcha","affected_versions":">=0.0.1"},{"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.","message":"The 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.","severity":"breaking","affected_versions":">=2.0.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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`.","cause":"Attempting to call `Queue` directly without `new`, or incorrect CommonJS `require` syntax in an ES module context.","error":"TypeError: Queue is not a constructor"},{"fix":"Ensure that every function you add to the queue explicitly calls `this.next()` once its operation is finished, particularly for asynchronous tasks.","cause":"The functions added to the queue are not calling `this.next()` upon completion.","error":"Queue stalls, functions are added but never execute subsequent items."},{"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.","cause":"Attempting to use `require()` in an ES module environment without a bundler, or `import` in a CommonJS environment without Babel/TypeScript.","error":"ReferenceError: require is not defined or SyntaxError: Cannot use import statement outside a module"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null}