I wanted something simple enough even I could use it =)
Run some functions in parallel, storing their result in the properties of a single result object. When there is an error or everything is done running in parallel, invoke my callback.
var DS = require("DeskSet");
var set = new DS(cb);
set.add(propertyName, asyncfn, [arg1, arg2... argN]);
set.add(propertyName2, asyncfn2, [arg1, arg2... argM]);
var Set = require("DeskSet");
//Define some Async functions.
function getUser(id, cb){
process.nextTick(function(){ //use redis or mongo or couch..
cb(null, {name:"Aaron"});
})
};
function getSomeData(filename, cb){
fs.readFile("./"+filename, cb); //security hole!!
}
function render(name, data){
console.log(sys.inspect(data));
}
routes.get("/home", function(req, res){
var set = new Set(function(err, result){
if(err){
process.exit(); //oops!
}
res.end(render("home", result));
});
set.add("user", getUser, req.params.id);
set.add("someData", getSomeData, "myfilename");
set.add("someOtherData", getSomeData, "myOtherFilename");
});
// This will result in
{
user: {
name: "Aaron"
},
someData: "contents of myfilename",
someOtherData: "contents of myOtherFilename"
}
Anyway, I have found that this is the easiest way for my feeble brain to do parallel tasks.
var DS = require("DeskSet");
var set = new DS(cb);
set.add(propertyName, fn, [arg1, arg2... argN]);
set.add(propertyName2, fn2, [arg1, arg2... argM]);
Create a new DeskSet object by passing in a callback, that is a function that accepts err, result
as its arguments.
Your callback will be invoked once for each error and once when all functions have returned.
The error passed to your callback is in the form of {property: propertyName, error: err}
.
property
is populated with the propertyName you passed to add
when adding the function to the DeskSet.
error
is the error that the function passed.
Your callback will be called when all of the other functions have completed, regardless of any errors.
err
will be null
.
result
is a plain javascript object where each property is assigned the result of its corresponding functions.
fn
is a function in the form of function([arg1, arg2... argN], cb)
where the cb is expected to be of the type: function(err, result){}
propertyName
is the key that the result of this function invocation will be stored in.
Probably not. DeskSet is super simple. There is no timeout, automatic try/catch wrapping, or cake.