How to call another function in async waterfall in Node.js?

1.2k views Asked by At

I just used the async waterfall method but I am confused about calling the function after another. I want to console 'hi' but it is not getting called. Can anyone please suggest help?

 exports.inviteAgent = function (req, res) {
      var obj = {};
      async.waterfall([
        function (done) {
          var url = config.mailer.inviteAgentUrl;
          res.render('modules/users/server/templates/invite-agent', {
            name: obj.name,
          }, function (err, emailHTML) {
            done(err, emailHTML);
          });
        },
        // If valid email, send reset email using service
        function (emailHTML, done) {
          var mailOptions = {
            to: obj.email,
            from: config.mailer.from,
            subject: 'Invite agent',
            html: emailHTML
          };
          smtpTransport.sendMail(mailOptions, function (err) {
            if (err) {
              return res.send({ 'respCode': common.statusCodes.COMMON, 'respMessage': errorHandler.getErrorMessage(err) });
            } else {
              res.jsonp({ 'respCode': common.statusCodes.SUCCESS, 'respMessage': 'Send email successfully' });
              done(done);
            }
          });
        },
        function (done) {
    console.log('hi')
        }
      ], function (err) {

      });
    };
3

There are 3 answers

0
Anuroop Kuppam On

I think you are not handling the callbacks or done functions in this case properly. Try this out

exports.inviteAgent = function (req, res) {
var obj = {};
async.waterfall([
    function (done) {
        var url = config.mailer.inviteAgentUrl;
        res.render('modules/users/server/templates/invite-agent', {
            name: obj.name,
        }, function (err, emailHTML) {
            done(err, emailHTML);
        });
    },
    // If valid email, send reset email using service
    function (emailHTML, done) {
        var mailOptions = {
            to: obj.email,
            from: config.mailer.from,
            subject: 'Invite agent',
            html: emailHTML
        };
        smtpTransport.sendMail(mailOptions, function (err) {
            if (err) {
                res.send({ 'respCode': common.statusCodes.COMMON, 'respMessage': errorHandler.getErrorMessage(err) });
                done(err);
            } else {
                res.jsonp({ 'respCode': common.statusCodes.SUCCESS, 'respMessage': 'Send email successfully' });
                done();
            }
        });
    },
    function (done) {
        console.log('hi');
        done();
    }
], function (err) {
    if(err) console.log(err);
});
0
Igor Skoric On

You need to be more careful with your variable names. I have refactored your code to differentiate your usage of the name "done".

exports.inviteAgent = function (req, res) {
    var obj = {};
    async.waterfall([
        function (callback1) {
            var url = config.mailer.inviteAgentUrl;
            res.render('modules/users/server/templates/invite-agent', {name: obj.name,}, function (err, emailHTML) {
                callback1(err, emailHTML);
            });
        },
        // If valid email, send reset email using service
        function (emailHTML, callback2) {
            var mailOptions = {};
            smtpTransport.sendMail(mailOptions, function (err) {
                if (err) {
                    return res.send({ 'respCode': common.statusCodes.COMMON, 'respMessage': errorHandler.getErrorMessage(err) });
                } else {
                    res.jsonp({ 'respCode': common.statusCodes.SUCCESS, 'respMessage': 'Send email successfully' });
                    callback2(callback2); // <== ERROR HERE
                }
            });
        },
        function (callback3) {
            console.log('hi')
        }
    ], function (err) {});
};

You call the callback2 function with itself. This parameter value is not null, which means the callback error parameter is not null. That leads to abortion of the waterfall and immediate call of the error part of the waterfall function.

0
Sushil Kumar On

when you are returning callback done() from internal functions of waterfall, If there is error you have to call done(err). And if there is not err call done(null, data) data contains arguements send to next function. Because the first parameter is err in done() if it is null then it goes to next function other wise it stops the execution of waterfall.