https://github.com/microfleet/callback-queue
Put your callbacks into queue to make sure that concurrent requests that you might want to perform will only be executed once
https://github.com/microfleet/callback-queue
Last synced: 9 months ago
JSON representation
Put your callbacks into queue to make sure that concurrent requests that you might want to perform will only be executed once
- Host: GitHub
- URL: https://github.com/microfleet/callback-queue
- Owner: microfleet
- License: mit
- Created: 2015-03-17T22:05:09.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2025-01-19T06:17:25.000Z (about 1 year ago)
- Last Synced: 2025-06-02T10:59:45.882Z (10 months ago)
- Language: TypeScript
- Size: 905 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Callback queue
[](https://www.codacy.com/app/v/callback-queue)
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
```js
const callbackQueue = require('callback-queue');
const request = require('request');
function performOutgoingRequest(url, next) {
const callback = callbackQueue.add(url, next);
if (!callback) {
return;
}
request(url, callback);
}
for (let i = 0; i < 100; i++) {
// request itself will be performed just once
performOutgoingRequest('https://google.com', function niceCallback(err, resp, body) {
// will be called 100 times
});
}
for (let x = 0; x < 100; x++) {
// request itself will be performed just once
performOutgoingRequest('https://google.com', function niceCallback(err, resp, body) {
// will be called 100 times with Error object
});
}
callbackQueue.remove('https://google.com', new Error('cancel it!'));
```