I am attempting to create a minimal example where I can accomplish what I describe above. For this purpose, here is my attempt for a minimal example, where in the end I would like to see in the output
negative of 1 is -1
plus one of 2 is 3
Here is my code.
var async = require('async');
var i, args = [1, 2];
var names = ["negative", "plusOne"];
var funcArray = [
function negative(number, callback) {
var neg = 0 - number;
console.log("negative of " + number + " is " + neg);
callback(null, neg);
},
function plusOne(number, callback) {
setTimeout(function(number, callback) {
var onemore = number + 1
console.log("plus one of " + number + " is " + onemore);
callback(null, onemore);
}, 3000);
}];
var funcCalls = {};
for (i = 0; i < 2; i++) {
funcCalls[names[i]] = function() {
funcArray[i].apply(this, args[i]);
};
}
async.parallel(funcCalls, function(error, results) {
console.log("Parallel ended with error " + error);
console.log("Results: " + JSON.stringify(results));
});
Note that I am passing a named object to async.parallel as well. Passing an array (and forgetting entirely about the names) would also work as an answer for me, but I am more interested in passing such an object.
Any ideas on achieving my goal?
f.nameif that's what you need... But I'm not sure I understand what you want to do.negative(1, cb)andplusOne(2, cb). Makes any sense? The idea is that during registration of some peers I want to create some objects in different collections in parallel. I can write down the (single) argument that I intend to pass to such a function, and then call them with async.parallel. It would make the code much cleaner. Makes sense what I am trying to say?