Hans Hans - 3 months ago 16
Node.js Question

Node async parallel inside waterfall

I'm building a server in Nodejs to retrieve data from some database. I've worked with the async library for some time now and figured out some things like putting a waterfall inside a parallel function.

I stumbled upon a problem where I first need to execute a query and then use that query's result in other queries that can be executed concurrently. The code looks something like this:

async.waterfall([
function(callback) {
connection.query( query,
function(err, rows, fields) {
if (!err) {
callback(null,rows);
} else {
callback(null,"SORRY");
}
}
);
},
async.parallel([
function(resultFromWaterfall,callback) {
connection.query(query,
function(err, rows, fields) {
if (!err) {
callback(null,rows);
} else {
callback(null,"SORRY");
}
}
);
},
function(resultFromWaterfall,callback) {
connection.query(query,
function(err, rows, fields) {
if (!err) {
callback(null,rows);
} else {
callback(null,"SORRY");
}
}
);
}
])
], finalCallback
);


Now my problem is to access the result from the waterfall function and to use it in the parallel functions.

Answer
async.waterfall([
    function(callback) {
        connection.query(query,
            function(err, rows, fields) {
                if (!err) {
                    callback(null, rows);
                } else {
                    callback(null, "SORRY");
                }
            }
        );
    },
    function(prevData,callback){
      console.log(prevData);//Use it whereever you want.
      async.parallel([
          function(callbackOfAsyncParallel) {
              connection.query(query1,
                  function(err, rows1, fields1) {
                      if (!err) {
                          callbackOfAsyncParallel(null, rows1);
                      } else {
                          callbackOfAsyncParallel(null, "SORRY1");
                      }
                  }
              );
          },
          function(callback) {
              connection.query(query2,
                  function(err, rows2, fields2) {
                    if (!err) {
                        callbackOfAsyncParallel(null, rows2);
                    } else {
                        callbackOfAsyncParallel(null, "SORRY2");
                    }
                  }
              );
          }
      ],function mainCBOfParallel(err,reuslts){
        if(!err){
          //this will be done after tasks in async.parallel are finished.
          callback(null,results);
          //results[0]===>rows1
          //results[1]===>rows2
        }
      });
    }
], finalCallback);

You have two mistakes in your code,

  1. tasks should be a function to execute.
  2. async.parallel has only callbacks in its task functions.

Update

There are callbacks (callbackOfAsyncParallel) which will be called when a task in async.parallel is finished.

It should not invoke callback (callback) of async.waterfall.If done probably there may be error/unexpected results.

Comments