javascript - Node.js Async Series not working in order -


i trying perform 2 loops in series, 1 before other using async node.js module.

async.series([     insertskill,//first loop     insertbehaviours//second loop ], function(err, results){         console.log(results);//print results     }); 

this code in each function, have removed code better readability

function insertskill(fncallback){     async.eachseries(object.keys(behaviours), function (askill, callback){         if (askill.indexof('skillid') > -1) {             if (behaviours[askill] == null || behaviours[askill] == "") {}              connection.get().query('select skill_id skills skill_id = ?', num, function (err, skillresults) {                  if (skilltitle != null || skilltitle != "") {                      connection.get().query('insert skills set ?', [skill], function (err, skillresults) {                          if (err) {}                         else {                             console.log("1");//printing after 2, dont want that!!!                        }                     });                 }             });         }         callback(null);     },fncallback); } 

second function

//second function function insertbehaviours(fncallback){     async.eachseries(object.keys(behaviours), function (abehaviour, secondcallback) {         if (abehaviour.indexof('behaviourid') > -1) {             if (behaviours[abehaviour] == null || behaviours[abehaviour] == "") {             console.log("2");//printing before 1, dont want that!!!                  });             }         }         secondcallback(null);     },fncallback); } 

the issue is, second function insertbehaviours happening first before insertskills. want insertskills happen first , before insertbehaviours

the reason first function, insertskill has couple of asynchronous methods in it. however, you're calling callback on iterator before methods have chance complete. insertskill method should more this:

function insertskill(fncallback){     async.eachseries(object.keys(behaviours), function (askill, callback){         if (askill.indexof('skillid') > -1) {             if (behaviours[askill] == null || behaviours[askill] == "") {}              connection.get().query('select skill_id skills skill_id = ?', num, function (err, skillresults) {                  if (skilltitle != null || skilltitle != "") {                      connection.get().query('insert skills set ?', [skill], function (err, skillresults) {                          if (err) {}                         else {                             console.log("1");//printing after 2, dont want that!!!                                                      callback(null); // notice moved this.                           }                     });                 }             });         }     },fncallback); } 

as call callback function once 2 sql statements have run.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -