parallizer
v2.0.1
Published
asynchronous functions helper
Downloads
4
Maintainers
Readme
Parallizer
Parallizer is a tool that makes working with asynchronous functions much easier.
Installation
$ npm install parallizer --save
Example
var Parallizer = require('parallizer')
// creates a new Parallizer object
// that will only run 3 functions at the same time.
var prl = new Parallizer(3)
// very important: last argument must be the callback.
var add2 = function (id, rnd, cb) {
setTimeout(cb.bind(null, id, rnd), 100)
}
for (var i = 0; i < 100; i++) {
var rnd = Math.floor(Math.random() *500)
prl.sadd(add2, 'ID#' + i, rnd, function (id, rnd) {
console.log(id + ': ' + rnd)
})
}
Documentation
new Parallizer([max], [cb], [paused])
Creates an new Parallizer object.
Arguments
max
- The maximum concurrent running functions. (Default: 1)cb
- The callback that will be called if all functions have been executed.paused
- The queue is initially paused.
parallizer.sadd(fn[, arg1[, arg2[, ...]]])
Adds a function to the queue an executes it if possible.
Arguments
fn
- The function to be called, last argument must be a callback.arg1, arg2, ...
- Arguments forfn
.
parallizer.add(fn, [args], [cb], [scope], [high])
This function does the same like parallizer.sadd
, but with a different API.
Arguments
fn
- The function to be called, last argument must be a callback.args
- An array specifying the arguments with whichfn
should be called.cb
- Callback (last argument) offn
.scope
- The scope (this reference) in which thefn
is executed.high
- Iftrue
, pushesfn
in front of the queue.
parallizer.start()
Starts the paused queue.
parallizer.pause()
Pauses the queue.
Tests
$ npm install && npm test
Licence
Copyright © 2015 Christoph Witzko