Wednesday 15 May 2013

node.js - Javascript async callback hell -



node.js - Javascript async callback hell -

i have application used async avoid "spaghetti callback" , worked properly, in parallel calls had create changes because values have changed before returning, these changes made same routine thought create function , save code, not work application (i'm new javascript , i'm learning).

debug (console.log), error in cases same callback called different requests (if local variables not understand how happens). have tried alter code foreach , async.each, in both cases have errors , no longer more alter maintain trying, can't find fault.

my original code (i summarize avoid long post):

async.parallel({ today: function(callback){ data.get('data', function(err, dataget){ if(err){ callback(err); } callback(null, dataget); }); }, .... yesteday, week, month .... year: function(callback){ data.get('data', function(err, dataget){ if(err){ callback(err); } callback(null, dataget); }); } }, function(error, results){ --- routine ---- });

and new code this:

function

function getdatachange(key, valuepass, callback){ var values = [ .... ], totaldata = 0.00; /* async.each(values, function(value, cb){ var keyr = key.replace(/%value%/g, value.tolowercase() ); data.get(keyr, function(err, dataget){ if(err){ cb(err); } dataget = ( dataget !== null ) ? dataget : 0 ; if( valuepass === value ) { totaldata += parsefloat( dataget ); cb(); } else { valueconverter.convert({ force: true, multi: true }, function(data){ totaldata += parsefloat( info ); cb(); }); } }); }, function(err){ if(err){ callback(err); } else { callback(null, totaldata ); } }); */ var totals = values.length; values.foreach(function(value){ var keyr = key.replace(/%value%/g, value.tolowercase() ); data.get(keyr, function(err, dataget){ if(err){ homecoming callback(err); } dataget = ( dataget !== null ) ? dataget : 0 ; total--; if( valuepass === value ) { totaldata += parsefloat( dataget ); if( totals === 0 ){ callback(null, totaldata); } } else { valueconverter.convert({ force: true, multi: true }, function(data){ totaldata += parsefloat( info ); if( totals === 0 ){ callback(null, totaldata); } }); } }); }); //callback(null, totaldata); }

and changes principal routine:

var keybase = '......', value = '.....'; async.parallel({ today: function(callback){ /* data.get('data', function(err, dataget){ if(err){ callback(err); } callback(null, dataget); }); */ getdatachange(keybase + 'datad', value, function(err, returndata){ if(err){ callback(err); } //console.log('## homecoming info today'); callback(null, returndata); }); //callback(null, 0); }, .... yesteday, week, month .... year: function(callback){ getdatachange(keybase + 'datay', value, function(err, returndata){ if(err){ callback(err); } console.log('## homecoming info year'); callback(null, returndata); }); } }, function(error, results){ --- routine ---- });

i think i'll have duplicate code introducing in parallel async calls, since not able operate.

my errors varied. in various tests commented phone call function , have established returned callback 0, in several parallel calls, , have seen not running final async.parallel callback function (for example, if commented calls except today, yesterday , week). in other cases (establishing calls comment , setting value 0, except @ yesterday , week), runs twice week callback. if calls function commented, except week , today, causes exception showing message "error: callback called".

i've been stuck several days , can not find error or how solve problem :-s

thanks you.

fixed !

one of modules used function called request web api, much delayed response.

javascript node.js asynchronous callback

No comments:

Post a Comment