See the following nodejs code with async:
var async = require('async');
function inc(n, cb) {
setTimeout(function() {
cb(null, n+1);
},1000);
};
var calls = [];
for(i=0;i<3;i++) {
calls.push(function(cb){
inc(i, cb);
});
}
async.parallel(calls, function(err, results) {
console.log(results);
});
It prints:
[4, 4, 4]
I don't understand why the result isn't [1, 2, 3]
?