Async - passing variables and preserving context
asynchronous, javascript, node.js
Solution
var asyncConfig = {};
var a, b;
for(var i = 0; i < someValue; i++) {
// do something with a
// do something with b
(function(a,b){
asyncConfig[i] = function(callback) {
func(a, b, callback); // func is async
}
})(a,b);
}
// Include some more parallel or series functions to asyncConfig
async.auto(asyncConfig);
Problem
If you have the following code : ``` var asyncConfig = {}; var a, b; for(var i = 0; i < someValue; i++) { // do something with a // do something with b asyncConfig[i] = function(callback) { func(a, b, callback); // func is async } } // Include some more parallel or series functions to asyncConfig async.auto(asyncConfig); ``` - How can you pass the values of the variables `a` and `b` to `func` so that when `async.auto(asyncConfig)` is executed after the `for` loop, the context of `a` and `b` is preserved ? (Different context of `a` and `b` for every execution of `func`.) Thank you in advance !