0.0.5 • Published 8 years ago
batch-manager v0.0.5
batch-manager
Easily batch the execution of functions as promises.
Installation
$ npm install --save batch-manager
Usage
var BatchManager = require('./BatchManager');
var Q = require('q');
var httpGet = function (uri, options) {
return Q.denodeify(require('request').get)(uri, options)
.spread(function (resp) {
return resp;
});
};
// Create a new `BatchManager` object, with a 5 task batch size.
var bm = new BatchManager(5);
for (var i = 0; i < 20; i++) {
// Add functions to the `BatchManager`'s internal queue.
bm.add(httpGet, ['http://api.randomuser.me/', { qs: { gender: 'female' } }]);
}
// Launch the `BatchManager` into action - the queued set of functions are invoked in batches.
bm.run()
.then(function (responses) {
responses.map(function (resp, index) {
console.log('Request No. ' + index + '. \tResponse Status: ' + resp.statusCode);
});
});
License
ISC © Rajiv Bose