Queue up your Promises to be executed in sequence. Also supports wait-time between Promise executions.
Promise queue to execute promises in a serial fashion with an optional wait-time between the executions.
OS X, Windows & Linux
npm install node-promise-serial --saveSimple usage with function* to queue up promises to be executed in serial.
const PromiseQueue = require('promise-queue');
// Example generator function to create Promises to be executed by the queue.
// This could be yielding any type of Promise (e.g. request-promise)
function* promiseGenerator() {
for(let i = 0; i < 10; i++) {
yield new Promise(resolve => {
setTimeout(resolve, (Math.random()*100)+1);
});
}
}
const pq = new PromiseQueue(promiseGenerator, 100);
// Listen on the events emitted from the PromiseQueue.
// 'resolved' - successfully resolved promise.
// 'rejected' - the Promise was rejected.
// 'completed' - the generator is completed (no new Promises to consume)
pq.on('resolved', res => {
console.log(`Result that was resolved: ${res}`)
}).on('rejected', err => {
console.log(`Error occured: ${err}`);
}).on('completed', () => {
console.log('Generator is completed.');
});
For a complete tutorial on how this was created have a look at my blog: https://www.snappyjs.com
No dependencies needed, just start testing your code see below
npm install node-promise-serial --save
npm test
npm start- 1.0.0
- RELEASE: Initial release.
Tommy Dronkers – Twitter: @snappyJS – E-mail: tommy@snappyjs.com Homepage: https://www.snappyjs.com
Distributed under the MIT license. See LICENSE for more information.
https://github.com/snappyjs/node-promise-serial
- Fork it (https://github.com/snappyjs/node-promise-serial/fork)
- Create your feature branch (
git checkout -b feature/fooBar) - Commit your changes (
git commit -am 'Add some fooBar') - Push to the branch (
git push origin feature/fooBar) - Create a new Pull Request