Library for limiting parallel work.
npm install --save command-pool
Static method .start (returns a Promise):
CommandPool.start(commandArguments, [parallelCount], callback);
- commandArguments (Array) - Array of arguments for commands or count of iterations;
- parallelCount (Optional, Number, Default = 1) - Count of parallel work tasks;
- callback (Function(arg, i, next)) - Function that calls on task, you must return a Promise or call next();
CommandPool.start(tasksCount, [parallelCount], callback);
- tasksCount (Number) - Count of iterations;
- parallelCount (Optional, Number, Default = 1) - Count of parallel work tasks;
- callback (Function(i, next)) - Function that calls on task, you must return a Promise or call next();
var CommandPool = require('command-pool');
CommandPool.start(5, 3, function(i, next) {
console.log('%s started', i);
setTimeout(function() {
console.log('%s resolved', i);
next(null, 'OK');
}, Math.floor(Math.random() * 2000));
}).then(function(data) {
console.log('RESULT:', data);
}, function(error) {
console.log('ERROR:', error);
});
0 started
1 started
2 started
0 resolved
3 started
1 resolved
4 started
3 resolved
2 resolved
4 resolved
RESULT: [ 'OK', 'OK', 'OK', 'OK', 'OK' ]
Work only in node version >= 0.12.