0

My goal is to execute some javascript functions sequentially, where results from one functions often (but not always) used as arguments of another.

var async = require('async');

var commonFunction1 = function(arg1, arg2, callback) { // callback(err, res1, res2)
  callback(null, arg1 + arg2, 11);
}

var commonFunction2 = function(arg3, arg4, callback) { // callbackb(err, res3, res4)
  callback(null, arg3 + arg4, 90);
}

var argument1 = 1,
    argument2 = 2,
    result1,
    result2,
    result3,
    result4;

async.series({
  1: function(cb) {
    commonFunction1(argument1, argument2, function(err, res1, res2) {
      result1 = res1;
      result2 = res2;
      cb(err);
    });
  },
  2: function(cb) {
    commonFunction2(result1, result2, function(err, res3, res4) {
      result3 = res3;
      result4 = res4;
      cb(err);
    });
  },
}, function(err, results) {
  console.log(err, result1, result2, result3, result4);
});

As you see statements like result1 = res1 is a forced syntax sugar.

Is there a better way to link local variables and callback function arguments?

P.S. Closures are not my way, because I plan to move commonFunction1 and commonFunction2 to another module.

1 Answer 1

1

Use series as it is intended to be used,pass only one result to the next callback :

async.series([function(next) {
        commonFunction1(argument1, argument2, function(err, res1, res2) {
          next(err,{result1:res1,result2:res2});
        });
      ,
      function(next) {
        commonFunction2(result1, result2, function(err, res3, res4) {
          next(err,{result3:res3,result4:res4});
        });
      }],function(err,results){
             var finalObject= results.reduce(function(res,next){
                 Object.keys(next).forEach(function(k){
                    res[k]=next[k];
                 });
                 return res;
             },{});
             console.log(finalObject);
      });

I'd suggest you architecture your commFunctions better and return an object with properties instead of passing 2 results along with the error.

var commonFunction1 = function(arg1, arg2, callback) { // callback(err, res1, res2)
  callback(err, {prop1:arg1 + arg2, prop2:11} );
}

that's the best practice in nodejs,just look at async core functions, and will make your code easier to work with.That way you wont have to write all the reduce code in your final async.series callback,just use an array of results.

EDIT : as stated in my comment , async.waterfall would be better for your use case.

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

3 Comments

commonFunction2(result1, ... result1 is undefined here
sure,but do you get the gist of what i'm telling you here?that's the point.My point is that your current strategy will lead to complex code at first place.
ha,you mean you need to pass results from previous async operations?then use waterfall instead of series.i'll correct the code later.

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.