Registry / devops / pending-queue

pending-queue

JSON →
library1.2.1jsnpmunverified

A lightweight queue that ensures a given asynchronous function (load) is executed only once for a given set of parameters, while multiple callers can await the result. Version 1.2.1 is the latest stable release. The package is minimal (zero dependencies) and designed for Node.js 4+. It provides an EventEmitter-based API and custom key serialization via stringify. Use cases include deduplicating API calls, database queries, or any expensive async operation. Unlike similar deduplication utilities, pending-queue queues listeners rather than memoizing results, making it suitable for one-shot or short-lived caching.

npm install pending-queue
INSTALL
IMPORT
SIG · PENDING-QUEUE
P
pending-queue
devopsjavascriptv1.2.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

Queue
const Queue = require('pending-queue')
import Queue from 'pending-queue'
This package does not provide ESM exports; use require() in Node.js.
Queue (with options)
const queue = new (require('pending-queue'))({ load: fn })
const queue = require('pending-queue').default({ load: fn })
The module exports a constructor directly; no default or named exports.
Queue add method
queue.add(1, 2).then(console.log)
queue.add([1, 2]).then(console.log)
add() takes spread arguments, not an array; it will pass them to the load function.

Demonstrates deduplication: three calls to .add(1,2) share a single execution of the load function.

const Queue = require('pending-queue'); const queue = new Queue({ load: (a, b) => new Promise(resolve => setTimeout(() => resolve(a + b), 100)) }); queue.add(1, 2).then(value => console.log('First:', value)); queue.add(1, 2).then(value => console.log('Second:', value)); queue.add(1, 2).then(value => console.log('Third:', value)); // All three resolve with 3, but load runs only once.
Debug
Known issues
gotchaParameters must be serializable deterministically (default uses JSON.stringify). Non-JSON-safe objects (dates, regex, etc.) may cause mismatched keys.
fix
Provide a custom stringify function or use addWithKey with an explicit key.
affects: >=1.0.0
gotchaThe queue does not expire; if load never resolves or rejects, .add() promises will hang forever.
fix
Ensure load always settles or implement a timeout externally.
affects: >=1.0.0
gotchaEvent names are derived from serialized keys (default JSON string). If keys are long or contain special characters, they may interfere with EventEmitter behavior.
fix
Use addWithKey with a safe key string.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: pending_queue is not a constructor
Using ESM import syntax with this CommonJS-only module.
fix
Replace `import Queue from 'pending-queue'` with `const Queue = require('pending-queue')`.
TypeError: queue.add is not a function
Calling `add()` before instantiating the Queue (e.g., `const queue = require('pending-queue').add(...)`).
fix
Instantiate with `new Queue({ load: ... })` then call `.add()`.
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: x): Error
The load function threw an error or returned a rejected promise, and no .catch() is attached to the add() promise.
fix
Attach a .catch() to each .add() call or use the 'error' event on the queue.
Upgrade
Version history
1.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
pending-queue — npm install pending-queue · libregistry