1.0.4 • Published 2 years ago
promise-dispatcher v1.0.4
PromiseDispatcher
promise-dispatcher
A lightweight library to manage promises according to settings such as processing order, time interval and rate per time interval.
Features
- Promise processing mode - management of the promises dispatching order ( FIFO queue or LIFO stack )
- Time Interval - time interval used to calculate promise dispatching rate ( default to 1 second )
- Rate dispatching - management of the promise dispatching rate ( max promise per time interval )
Installation
$ npm install promise-dispatcher
Usage
Loading the module
const PromiseDispatcher = require('promise-dispatcher');
Creating a new promise dispatcher instance
const promiseDispatcher = new PromiseDispatcher(tasks, mode = "QUEUE", rate = 1, interval = 1000)
Dispatch a new promise
To dispatch a new promise in a promise dispatcher instance, it must be wrapped into a provider function that return the promise so that it can be executed whenever necessary by the promise dispatcher
const promiseProvider = function() {
return new Promise((resolve,reject) => {
// do stuff
resolve()
})
}
promiseDispatcher.dispatchPromise(promiseProvider)
startDispatching
A promise dispatcher instance starts to process promises once its startDispatching method is called
promiseDispatcher.startDispatching()
stopDispatching
A promise dispatcher instance stops to process promises once its stopDispatching method is called
promiseDispatcher.stopDispatching()
Examples
const rate = 5 // promise rate per time interval
const interval = 1000 // time interval in ms
const promiseProviders = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(function(value) {
return function () {
return new Promise((resolve, reject) => {
console.log(value)
resolve(value)
});
};
});
Queue mode
(async function() {
const promiseDispatcher = promiseDispatcher.new("QUEUE", rate, interval)
promiseDispatcher.startDispatching()
const promises = promiseDispatcher.dispatchPromises(promiseProviders)
// expected console logs :
// after 0 second : 1,2,3,4,5
// after 1 second : 6,7,8,9,10
const results = await Promise.all(promises)
console.log(results)
// expect results to be : 1,2,3,4,5,6,7,8,9,10
promiseDispatcher.stopDispatching()
})();
Stack mode
(async function() {
const promiseDispatcher = promiseDispatcher.new("STACK", rate, interval)
promiseDispatcher.startDispatching()
const promises = promiseDispatcher.dispatchPromises(tasks)
// expected console logs :
// after 0 second : 10,9,8,7,6
// after 1 second : 5,4,3,2,1
const results = await Promise.all(promises)
console.log(results)
// expect results to be : 10,9,8,7,6,5,4,3,2,1
promiseDispatcher.stopDispatching()
})();
License
MIT
1.0.2
2 years ago
1.0.4
2 years ago
1.0.3
2 years ago
1.3.2
10 years ago
1.3.1
10 years ago
1.3.0
10 years ago
1.2.6
10 years ago
1.2.5
10 years ago
1.2.4
10 years ago
1.2.3
10 years ago
1.2.2
10 years ago
1.2.1
10 years ago
1.2.0
10 years ago
1.1.2
10 years ago
1.1.1
10 years ago
1.1.0
10 years ago
1.0.1
10 years ago
1.0.0
10 years ago