0
var Async = require('async');

var Test_Hander = function () {
};

Test_Hander.prototype.begin = function () {
    Async.series([
        this.first, // this.first.bind(this) does not work either
        this.second
    ],function (error) {
        if (error) {
            // shit
        }
        else {
            // good
        }
    });

};

Test_Hander.prototype.first = function (callback) {
    console.log('Enter first function');
    callback(null,'I am from first function');
};

Test_Hander.prototype.second = function (one, callback) {
    console.log('Enter second function');
    console.log('parameter one: ');
    console.log(one);
    console.log(callback);

    callback(null);
};

var hander = new Test_Hander();
hander.begin();

I want to pass some value from function first to function second. And I know that waterfall and global variable are both ok. However, can I just pass a result value from function first to function second without using waterfall and global variable?

yeyimilk
  • 614
  • 2
  • 8
  • 18
  • I've faced a similar situation and I've not come across a solution without adding a global variable. Link to duplicate question `https://stackoverflow.com/questions/22424592/nodejs-async-series-pass-arguments-to-next-callback/22424905` Why do you feel the need to use Async library when this can be achieved using Promise. – Vish Oct 23 '17 at 03:38
  • It's because of the historical code – yeyimilk Oct 23 '17 at 04:10
  • Ahh I c.. Then follow the accepted answer in the duplicate question and get it to work by declaring a variable. – Vish Oct 23 '17 at 04:12
  • @Vish From the your link answer, yes, I know it works, but if we have many functions, that solution may not good. – yeyimilk Oct 23 '17 at 04:12
  • If the requirement is execute the function in series with the arguments passed from first to second what's your concern with declaring the global variable? Here's the link to my discussion about the same issue from before. `https://stackoverflow.com/questions/14305843/how-do-i-pass-a-standard-set-of-parameters-to-each-function-in-an-async-js-serie/14306023#comment74056947_14306023` – Vish Oct 23 '17 at 04:23

1 Answers1

0

Using promise you can chain the methods and execute the code similar to async.series and you don't need to require a async module.

var Test_Hander = function () {
};

Test_Hander.prototype.begin = function () {
    this.first()
        .then(this.second)
        .catch(function(err){
            // Handle error
        });

};

Test_Hander.prototype.first = function (callback) {
    return new Promise(function(reject, resolve){
        // do something
        // if err : reject(err)
        // else : resolve(data);
    });
};

Test_Hander.prototype.second = function (responsefromFirst) {
    // Do somethign with response
};

var hander = new Test_Hander();
hander.begin();
Vish
  • 383
  • 2
  • 8
  • 25