javascript - RangeError: call stack exceed on async .eachSeries -


at last, actual stack overflow error reported on stackoverflow!

i following error in code below:

var m = patha.substr(-(pathb.length)); // var                                                ^ rangeerror: maximum call stack size exceeded 

i'm sure answer reported here, towards bottom:

https://github.com/caolan/async/issues/75

however, don't understand how fix code. not calling sync functions inside async functions, far know. can clarify have fix code?

i'm iterating on cross-product of result-set concatenate path strings 1 substring of other.

var = 0;     async.eachseries(results.rows, function (r, next2a) {         var patha = results.rows[i].id_path;         var j = 0;         async.eachseries(results.rows, function (r2, next1a) {             var pathb = results.rows[j].id_path;             //check i=j             if (!(i == j)) {                 var m = patha.substr(-(pathb.length)); // var m = (patha || '').substr(-((pathb) ? pathb.length : 0));                 if ((m == pathb) && (patha.length > pathb.length)) {                     logger.log('debug', (pathb + ' => ' + patha));                     conn.query("update user_token_details set id_l1=$1, id_l2=$2, id_l3=$3, id_l4=$4,id_l5=$5,id_path2=$9, id_path=$6 token_uuid=$7 , user_uuid=$8",                         [results.rows[i].id_l1, results.rows[i].id_l2, results.rows[i].id_l3, results.rows[i].id_l4, results.rows[i].id_l5, results.rows[i].id_path,                             results.rows[j].token_uuid, user_uuid, results.rows[j].id_path],                         function (error, result) {                             if (error) {                                 throw error;                             }                             j++;                             next1a();                          })                 } else {                     j++;                     next1a();                 }             } else {                 j++;                 next1a();             }           }, function () {             i++;             next2a();         });       }, function (err) { }); 

here form of spaghetti:

var = [0, 1, 2, 3, 4...300]; async.eachseries(a, function (a, next_a) {      async.eachseries(a, function (b, next_b) {        // "range error: maximum call stack size exceeded"         dosomethingasync(a,b, function () {         next_a();         });      }, function (err) {         next_b();     })  }, function (err) {     // resume }) 

the problem async.eachseries behaves asynchronously if callback inside called asynchronously. in case, last 2 calls next1a not performing query, occur synchronously, , extend call stack. in case, iterating enough hit max stack depth. simplest fix call next1a asynchronously.

replace each instance of

next1a(); 

with

setimmediate(next1a); 

except 1 async because of query. note while process.nexttick(next1a) work, has potential block event loop processing other tasks. because process.nexttick queues callback microtask, whereas setimmediate queues callback macrotask.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -