1

I am not an expert in node and async.js. So expecting help from node js community. So here is my scenario, I want to do 3 operation one after one, each depends on previous result. By using previous result i need to call async function to retrieve another result. So I decided to go with async waterfall. (Hope I chose correct one).

async.waterfall([
  function(callback) {
    request({
            method: 'GET',
            headers: {'Content-Type' : 'application/json'},
            url    : url
        },function(error, response, body){
              if(error) {
                  callback(error);
              } else {
                   var result= JSON.parse(body);
                   callback(null,result); //sending to next function
              }
        });
  },
  function(result, callback) {
    //here i want to use the result array in loop async.eachSeries or forEachSeries and fetch another result array using request module then send it to next function
    /*Here I don't know How to process the result*/
    callback(null,result1)
  },
  function(result1, callback) {
    //here i want to use the result1 array and fetch another result array then send it to next function
    callback(null,result2)
  }
], function(error, res) {
  console.log(res); // process final result 
});

I referred some tutorials. I couldn't understand that's why end up here in SO. Thanks in advance.

3
  • 1
    I want to help you but I really can't understand what's your question Commented Oct 16, 2015 at 14:55
  • Hi, I succeed in first step, (sending result to second function).I really don't know how to process in second step using loops and callback Commented Oct 16, 2015 at 14:58
  • I' responding now, please try to edit your own question being more explicit about your question Commented Oct 16, 2015 at 15:07

3 Answers 3

2

You just gotta use each cb passed in on every function in the waterfall function series.

Btw I feel forced to say:

  1. Don't nest async functions
  2. Try to use promises instead
  3. Always check around for whole async module functions, many times, some stuff can be run in parallel

Ok, so for waterfall function, basically requires the following syntax, semantically:

async.waterfall(arrayOfFunctions, [optionalResolveCallbackFunction]);

the first element in array of function has the following syntax:

function (cb) {...};

the following n functions will require following syntax:

function (param1, param2, ... paramN, cb){...}

So, last parameter will be the one used to go to next function or return an error. The number of waterfall params is optional.

Each cb function follows the error callback convention, where error is first parameter to be passed. If an error is returned at any function along the array, the execution will be interrupted and code will go to last optionalResolveCallbackFunction.

So, again, even that will produce complicated code (or even risk of performance issues prone) when you decide you've got termination of your loop async.eachSeries or forEachSeries, you will use the callback object which happens to be the callback param from the waterfall function to either pass to following function or end the execution.

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

Comments

2

This is an example of how it works:

async.waterfall([
    function(callback) {
        request({
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            },
            url: url
        }, function(error, response, body) {
            if (error) {
                callback(error);
            } else {
                var result = JSON.parse(body);
                callback(null, result); //sending to next function
            }
        });
    },
    function(result, callback) {
        //here i want to use the result array in loop async.eachSeries or forEachSeries and fetch another result array using request module then send it to next function
        async.each(result, function(item, callback) {
            request({
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json'
                },
                url: url
            }, function(error, response, body) {
                if (error) {
                    callback(error);
                } else {
                    var result = JSON.parse(body);
                    callback(null, result);
                }
            });
        }, function(err, result1) {
            if (!err) {
                callback(null, result1);
            } else {
                callback(err);
            }
        });
    },
    function(result1, callback) {
        request({
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            },
            url: url
        }, function(error, response, body) {
            if (error) {
                callback(error);
            } else {
                var result2 = JSON.parse(body);
                callback(null, result2);
            }
        });
    }
], function(error, res) {
    console.log(res); // process final result 
});

3 Comments

I think this is the solution i wanted. I will check this solution. Thanks for your time. Thanks a lot.
Hi i'm i tried print result1, got undefined console.log(result1)`
used asyncmapSeries instead async.each. it's worked
1

When the function call fail, you should return to make sure you don't get to the next step:

function(callback) {
    request({
            method: 'GET',
            headers: {'Content-Type' : 'application/json'},
            url    : url
        },function(error, response, body){
              if(error) {
                  return callback(error);
              } else {
                   var result= JSON.parse(body);
                   callback(null,result); //sending to next function
              }
        });
  },

The result param in the second function is the array you returned from the first call.

function(result, callback) {
    //here i want to use the result array in loop async.eachSeries or forEachSeries and fetch another result array using request module then send it to next function
    /*Here I don't know How to process the result*/
    for (var i in result) {
    ...
    }
    callback(null,result1)
  },

and then the same for the next function in the series

Comments

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.