{"action":"created","issue":{"url":"https://api.github.com/repos/caolan/async/issues/1104","repository_url":"https://api.github.com/repos/caolan/async","labels_url":"https://api.github.com/repos/caolan/async/issues/1104/labels{/name}","comments_url":"https://api.github.com/repos/caolan/async/issues/1104/comments","events_url":"https://api.github.com/repos/caolan/async/issues/1104/events","html_url":"https://github.com/caolan/async/issues/1104","id":146770471,"number":1104,"title":"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)","user":{"login":"alfredoborrero","id":9356338,"avatar_url":"https://avatars.githubusercontent.com/u/9356338?v=3","gravatar_id":"","url":"https://api.github.com/users/alfredoborrero","html_url":"https://github.com/alfredoborrero","followers_url":"https://api.github.com/users/alfredoborrero/followers","following_url":"https://api.github.com/users/alfredoborrero/following{/other_user}","gists_url":"https://api.github.com/users/alfredoborrero/gists{/gist_id}","starred_url":"https://api.github.com/users/alfredoborrero/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alfredoborrero/subscriptions","organizations_url":"https://api.github.com/users/alfredoborrero/orgs","repos_url":"https://api.github.com/users/alfredoborrero/repos","events_url":"https://api.github.com/users/alfredoborrero/events{/privacy}","received_events_url":"https://api.github.com/users/alfredoborrero/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-04-07T23:27:23Z","updated_at":"2016-04-08T01:41:59Z","closed_at":null,"body":"Currently I am trying to run in parallel the same function with different arguments using Node.JS\r\n\r\nFor this I use Async.js and i am struggling trying to push/stack functions to an array. \r\nThe problem is that the functions are executed with the same arguments. This is what I have:\r\n\r\n```js\r\nvar async = require(\"async\");\r\nvar array = [];\r\nvar x = [1,2,3,4];\r\n\r\n// This portion of code works perfect and the arguments are passed perfectly\r\n// The results that i am getting are: [100, 200, 300, 400]\r\narray.push(function(callback){ callback(null,calculate(x[0]))});\r\narray.push(function(callback){ callback(null,calculate(x[1]))});\r\narray.push(function(callback){ callback(null,calculate(x[2]))});\r\narray.push(function(callback){ callback(null,calculate(x[3]))});\r\n\r\n// This portion of code does not work and I dont know why ... \r\n// The results that i am getting are: [400, 400, 400, 400]\r\n// Obviusly the function is receiving the same argument a=4 everytime is called\r\nfor (i=0;i<3;i++){\r\n\tarray.push(function(callback){ callback(null,calculate(x[i]))});\r\n}\r\n\r\nasync.parallel(array,function(err,result){\r\n\tif (err) {\r\n\t\tconsole.log(err);\r\n\t\treturn;\r\n\t}\r\n\tconsole.log(\"Results are: \" + result);\r\n});\r\n\r\n\r\nfunction calculate(a) {\r\n  return a*100\r\n}\r\n```\r\n\r\nAny idea?\r\nThanks"},"comment":{"url":"https://api.github.com/repos/caolan/async/issues/comments/207164989","html_url":"https://github.com/caolan/async/issues/1104#issuecomment-207164989","issue_url":"https://api.github.com/repos/caolan/async/issues/1104","id":207164989,"user":{"login":"aearly","id":558190,"avatar_url":"https://avatars.githubusercontent.com/u/558190?v=3","gravatar_id":"","url":"https://api.github.com/users/aearly","html_url":"https://github.com/aearly","followers_url":"https://api.github.com/users/aearly/followers","following_url":"https://api.github.com/users/aearly/following{/other_user}","gists_url":"https://api.github.com/users/aearly/gists{/gist_id}","starred_url":"https://api.github.com/users/aearly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aearly/subscriptions","organizations_url":"https://api.github.com/users/aearly/orgs","repos_url":"https://api.github.com/users/aearly/repos","events_url":"https://api.github.com/users/aearly/events{/privacy}","received_events_url":"https://api.github.com/users/aearly/received_events","type":"User","site_admin":false},"created_at":"2016-04-08T01:41:59Z","updated_at":"2016-04-08T01:41:59Z","body":"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:\r\n\r\n```js\r\nasync.map([1, 2, 3, 4], function(x, callback){\r\n  callback(null,calculate(x))\r\n}, function(err,result){\r\n    if (err) {\r\n        console.log(err);\r\n        return;\r\n    }\r\n    console.log(\"Results are: \" + result);\r\n});\r\n```\r\n\r\nBut the real answer is that you  don't need Async at all:\r\n\r\n```js\r\nvar result = [1, 2, 3, 4].map(calculate)\r\n```"}}