Async: How can I dynamically push functions with different arguments to an array? I need to process those functions using Async.js (parallel or race mode)

Created on 8 Apr 2016  ·  4Comments  ·  Source: caolan/async

Currently I am trying to run in parallel the same function with different arguments using Node.JS

For this I use Async.js and i am struggling trying to push/stack functions to an array.
The problem is that the functions are executed with the same arguments. This is what I have:

var async = require("async");
var array = [];
var x = [1,2,3,4];

// This portion of code works perfect and the arguments are passed perfectly
// The results that i am getting are: [100, 200, 300, 400]
array.push(function(callback){ callback(null,calculate(x[0]))});
array.push(function(callback){ callback(null,calculate(x[1]))});
array.push(function(callback){ callback(null,calculate(x[2]))});
array.push(function(callback){ callback(null,calculate(x[3]))});

// This portion of code does not work and I dont know why ... 
// The results that i am getting are: [400, 400, 400, 400]
// Obviusly the function is receiving the same argument a=4 everytime is called
for (i=0;i<3;i++){
    array.push(function(callback){ callback(null,calculate(x[i]))});
}

async.parallel(array,function(err,result){
    if (err) {
        console.log(err);
        return;
    }
    console.log("Results are: " + result);
});


function calculate(a) {
  return a*100
}

Any idea?
Thanks

question

Most helpful comment

Lots of stuff here, including basic JS variable scoping quirks. The short answer to your question is that you can use async.map, it runs in parallel too:

async.map([1, 2, 3, 4], function(x, callback){
  callback(null,calculate(x))
}, function(err,result){
    if (err) {
        console.log(err);
        return;
    }
    console.log("Results are: " + result);
});

But the real answer is that you don't need Async at all:

var result = [1, 2, 3, 4].map(calculate)

All 4 comments

Lots of stuff here, including basic JS variable scoping quirks. The short answer to your question is that you can use async.map, it runs in parallel too:

async.map([1, 2, 3, 4], function(x, callback){
  callback(null,calculate(x))
}, function(err,result){
    if (err) {
        console.log(err);
        return;
    }
    console.log("Results are: " + result);
});

But the real answer is that you don't need Async at all:

var result = [1, 2, 3, 4].map(calculate)

Thank you very much ...

Sent from my iPhone

On Apr 7, 2016, at 7:42 PM, Alex Early [email protected] wrote:

Lots of stuff here, including basic JS variable scoping quirks. The short answer to your question is that you can use async.map, it runs in parallel too:

async.map([1, 2, 3, 4], function(x, callback){
callback(null,calculate(x))
}, function(err,result){
if (err) {
console.log(err);
return;
}
console.log("Results are: " + result);
});
But the real answer is that you don't need Async at all:

var result = [1, 2, 3, 4].map(calculate)

You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub

Hi Caolan,
Thank you so much for the help, it worked perfectly, however I would like to know how I can implement the "race" mode.
I already tried several times and I am getting errors.
I just want to run all my functions in parallel and get the result of whoever finishes first.
Any idea on how to implement Async.race ?
Regards,
Alfredo Borrero
Date: Thu, 7 Apr 2016 18:42:25 -0700
From: [email protected]
To: [email protected]
CC: [email protected]
Subject: Re: [caolan/async] How can I dynamically push functions with different arguments to an array? I need to process those functions using Async.js (parallel or race mode) (#1104)

Lots of stuff here, including basic JS variable scoping quirks. The short answer to your question is that you can use async.map, it runs in parallel too:

async.map([1, 2, 3, 4], function(x, callback){
callback(null,calculate(x))
}, function(err,result){
if (err) {
console.log(err);
return;
}
console.log("Results are: " + result);
});

But the real answer is that you don't need Async at all:

var result = [1, 2, 3, 4].map(calculate)


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub

Hi Caolan,

Thank you so much for the help, it worked perfectly, however I would like to know how I can implement the "race" mode. I already tried several times and I am getting errors.
I just want to run all my functions in parallel and get the result of whoever finishes first.
Any idea on how to implement Async.race ?

Regards,

Was this page helpful?
0 / 5 - 0 ratings