Skip to content

Put your callbacks into queue to make sure that concurrent requests that you might want to perform will only be executed once

License

Notifications You must be signed in to change notification settings

microfleet/callback-queue

Repository files navigation

Callback queue

Codacy Badge

Node.js callback queue. Can be used with browserify as well.

Idea is to queue callbacks, when same function wants to be called at the same time by different callers.

One example would be conserving resources spent on outgoing http requests.

npm install callback-queue -S

Example usage

  1. callbackQueue.add(String, Function): queues callbacks based on key attribute
  2. callbackQueue.remove(String, Error): calls queued callbacks with Error argument
constcallbackQueue=require('callback-queue');constrequest=require('request');functionperformOutgoingRequest(url,next){constcallback=callbackQueue.add(url,next);if(!callback){return;}request(url,callback);}for(leti=0;i<100;i++){// request itself will be performed just onceperformOutgoingRequest('https://google.com',functionniceCallback(err,resp,body){// will be called 100 times});}for(letx=0;x<100;x++){// request itself will be performed just onceperformOutgoingRequest('https://google.com',functionniceCallback(err,resp,body){// will be called 100 times with Error object});}callbackQueue.remove('https://google.com',newError('cancel it!'));

About

Put your callbacks into queue to make sure that concurrent requests that you might want to perform will only be executed once

Resources

License

Stars

Watchers

Forks

Packages

No packages published