0

Async.series is said to execute the function sequentially, but it does not for me,

My code:

var mysql=require('mysql');
var async=require('async'); 
var con;
var EID=30;
var res;
async.series([
    function(callback) {
      console.log("inside first func");
      con = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: '',
        database: 'imd2'
      });
      callback();
    },
    function(callback) {
      console.log("inside second function");
      var sql = "select EName from users where EID=" + EID;
      con.query(sql, function(err, rows) {
        console.log("inside query");
        res = rows;
      });
      callback();
    }
  ],
  function(err) {
    console.log("third function");
    if (err)
      return next(err);
    console.log(res);
  });
console.log("hi");

and my output is

inside first func
inside second function
third function
undefined
hi
inside query

If i change the second function's callback inside the con.query() Then the output changes to

inside first func
inside second function
hi
inside query
third function
[ RowDataPacket { EName: 'AndhraPradesh' } ]

My expected output is

inside first func
inside second function
inside query
third function
[ RowDataPacket { EName: 'AndhraPradesh' } ]
hi

Correct me if i'm wrong, but the function(err) should be called at the last, and then "hi" should be displayed right?(from the definitions given) What should I do to get my expected output?

P.S "hi" is just a debug, there will be very important codes after async.series to be performed and it will not be best fit inside a new function inside async.series Thanks

2
  • console.log("hi"); is outside of async.series call, with async series you manage the order of internal if you need to force order in the following functions calls you should included those into async.series Commented Jan 9, 2017 at 8:34
  • Ok, You just ignored the PS. part, The thing is, console.log("hi") is replaced by series of functions and statements which are synchronous by default and needs no extra change to it, If I need to incorporate all the functions inside async just for the second function, I believe that will be a bad quality program. The crux of this question is the sectond function, I believe there might be a different workaround in making mysql query syntax synchronous. Commented Jan 9, 2017 at 8:46

1 Answer 1

1

function(err) here is simply the callback of the async series. Everything you want to do after the series must be done here. Moving console.log("hi") in this function should help.

function(err) {
    console.log("third function");
    if (err)
        return next(err);
    console.log(res);
    console.log("hi");
});

The second point is that in an async series, callback() simply calls the next function. Now the thing is that you use another async method inside the second function of the series.

function(callback) {
    con.query(sql, function(err, rows) {
        console.log("inside query");
        res = rows;
    });
    callback();
}

con.query is an async task with a callback function. Async means that you don't know when the callback will be processed. So you need to call callback() inside the callback of con.query like so:

function(callback) {
    con.query(sql, function(err, rows) {
        console.log("inside query");
        res = rows;
        callback();
    });
}

This way the next function will be triggered only when you have received the result from your query (and logged "inside query").

Sign up to request clarification or add additional context in comments.

3 Comments

Again, you ignored the P.S part, console.log("hi") is actually a series of bigger statements, that should ideally be outside async.series function and ideally execute after async.series,and I've kept the callback function inside con.query() and also showed the result in the question(second output code)
@VishwanthIronHeart You can create a function outside the series with the code you want to execute after the series. Then in your function(err) just call this function. The code you showed had the callback function outside the con.query so I guessed you weren't sure were to put it. I just wanted to clear that out.
is there anyway to contact you personally? I'm sure even if this problem solves, there'll be 2 more forking ahead, and I'm having a hard time reading the documentation for nodejs... Also, regarding your comment, stackoverflow.com/questions/41521097/… . Refer this question by me, you'll understand why calling a function from callback of function(err) would not be best again. Because the snippet in this question will already be inside a function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.