6

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?

3
  • Why do you want to do that? Commented Oct 17, 2014 at 18:32
  • You can get the name of a function with f.name if that's what you need... But I'm not sure I understand what you want to do. Commented Oct 17, 2014 at 18:33
  • As the title says I want to pass a different argument to each function that I call with async.parallel. So, I want to run in parallel negative(1, cb) and plusOne(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? Commented Oct 17, 2014 at 18:36

2 Answers 2

15

Why not to bind the initial values? Then you would have something like this:

async.parallel([
    negative.bind(null, -1),
    plusOne.bind(null, 3)
], function(err, res) {
    console.log(err, res);
});

Of course you could generate the list with various parameters. This was just to give an idea of a simplified approach.

Sign up to request clarification or add additional context in comments.

2 Comments

Looks very good. I had the impression I tried bind but apparently I did not do it correctly. :) Of course I am accepting. Thank you for your time.
No probs. It's very important to remember to pass the context (ie. null for this) as otherwise it won't work. bind works well with async.
4

Instead you can use async.apply too

async.parallel(
[
  async.apply(negative, -1), //callback is automatically passed to the function
  async.apply(positive, 3)
],
(error, results) => {
console.log(error, results);
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.