Registry / devops / callback-queue

callback-queue

JSON →
library3.0.0jsnpmunverified

A Node.js library for queuing callbacks so that concurrent requests for the same resource are executed only once. Current stable version is 3.0.0. It allows you to deduplicate simultaneous calls to the same function (e.g., HTTP requests) by queuing callbacks under a key. Once the first call completes, all queued callbacks receive the result. Works in Node.js and browser via browserify. Lightweight and simple, with no external dependencies.

npm install callback-queue
INSTALL
IMPORT
SIG · CALLBACK-QUEUE
C
callback-queue
devopsjavascriptv3.0.0
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.

callback-queue
const callbackQueue = require('callback-queue');
import callbackQueue from 'callback-queue';
Package does not export ESM; CommonJS require is required.
add
const cb = callbackQueue.add(key, callback);
const cb = callbackQueue.add(key, callback, options);
No options parameter; only key and callback function.
remove
callbackQueue.remove(key, error);
callbackQueue.remove(key);
Second argument (Error) is required to trigger callbacks with error; omitting it does nothing.

Demonstrates deduplication of concurrent HTTP requests using callback queue.

const callbackQueue = require('callback-queue'); const request = require('request'); function performOutgoingRequest(url, next) { const callback = callbackQueue.add(url, next); if (!callback) { return; // another call is pending } request(url, callback); } for (let i = 0; i < 100; i++) { performOutgoingRequest('https://google.com', function(err, resp, body) { // called 100 times but only one actual request }); }
Debug
Known issues
gotchacallbackQueue.add() returns a callback function only if no other call is pending for the same key. If a call is already queued, it returns undefined and the caller should not proceed.
fix
Always check if the return value is callable before using it as a callback.
affects: >=0.0.0
gotchacallbackQueue.remove() must be called with an Error object as second argument to propagate errors. Omitting the error will not invoke the queued callbacks.
fix
Always pass an Error instance as the second argument to remove().
affects: >=0.0.0
breakingVersion 2.x changed the API from using 'put' and 'take' to 'add' and 'remove'. Code written for v1 will break.
fix
Use add() and remove() instead of put() and take().
affects: >=2.0.0 <3.0.0
gotchaThe library does not support promises or async/await natively. Callbacks must be used.
fix
Wrap in a Promise or use promisify from util.
affects: >=0.0.0
Errors
Common errors & fixes
callbackQueue is not a function
Calling require('callback-queue') returns an object, not a function. Some code mistakenly destructures add/remove as separate exports.
fix
Use const callbackQueue = require('callback-queue'); then callbackQueue.add() and callbackQueue.remove().
TypeError: callback is not a function
callbackQueue.add() returned undefined because another call with the same key is already pending, and the caller tried to use it as a callback.
fix
Check if callback is truthy before using it: if (!callback) return;
Cannot read property 'add' of undefined
Using ES module import (e.g., import callbackQueue from 'callback-queue') which is not supported.
fix
Use CommonJS require: const callbackQueue = require('callback-queue');
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
callback-queue — npm install callback-queue · libregistry