i using async library inside of project , end strange behavior : of arguments "undefined" in call.
for example (this not actual code behavior same , simpler expose problem way) :
var token; async.series([ function gettoken (done) { api.gettoken(userid, function (err, data) { if (err) return done(err); token = data; return done(); } }, async.apply(dosomethingwiththetoken, token) ], callback);
when function "dosomethingwiththetoken" called, token passed argument has "undefined" value, if changed in previous function "gettoken".
but if :
var token; async.series([ function gettoken (done) { api.gettoken(userid, function (err, data) { if (err) return done(err); token = data; return done(); } }, function dosomething (done) { dosomethingwiththetoken(token, done); } ], callback);
then no problem token passed in function "dosomethingwiththetoken" has correct value given previous function.
i've done little bit of research in async module before writing post , when display arguments passed async.apply function, surprise displayed before function call made in async.series flow - explains why token value "undefined".
is able explain me why behavior happening ?
i hope i've been clear issue in advance
whenever async.series called, prepares function references within array, adds gettoken in array, calls async.apply , adds generated function reference (output of async.apply) in array.
at time token undefined.
scenarios happening here this:
> var i; > var arr = [1,2,3,i,5,6]; > i=4; > arr [ 1, 2, 3, undefined, 5, 6 ]
hope understand this.
Comments
Post a Comment