nodejs and async.waterfall with if conditions and conditional function list.
Asked Answered
J

1

6

I have been working with async.waterfall and nodejs. Its working very well but now I have a question about flow.

I want to use a simple if condition in async.waterfall flow.

async.waterfall([
    callOne,
    callTwo,
        if(condition > 0 ) {
            callTest1,
            callTest2,
        }else{
            callTest3,
            callTest4,
        }
    callThree,
    callFour,
    callFive,
], function (err, result) {
    if (err) {
        return res.status(400).jsonp({error: err});
    }
});

I just want to test for one condition ..

If it is condition is true

then run a few functions

else

run other functions.

endif

cleanup

I was trying this too...one async.waterfall calling two async.waterfall/s

   router.post('/testUser', function (req, res, next) {

   ......


  function validateAccount(callback) {
    if (config.CHECK_EMAIL_MEMBER_ID > 0) {
                    async.waterfall([
                        callOne,
                        callTwo,
                            if(condition > 0 ) {
                                callTest1,
                                callTest2,
                            }else{
                                callTest3,
                                callTest4,
                            }
                        callThree,
                        callFour,
                        callFive,
                    ], function (err, result) {
                        if (err) {
                            return res.status(400).jsonp({error: err});
                        }
                    });
    } else {
                    async.waterfall([
                        callOneb,
                        callTwob,
                            if(condition > 0 ) {
                                callTest1b,
                                callTest2b,
                            }else{
                                callTest3b,
                                callTest4b,
                            }
                        callThreeb,
                        callFourb,
                        callFiveb,
                    ], function (err, result) {
                        if (err) {
                            return res.status(400).jsonp({error: err});
                        }
                    });
    }
}




async.waterfall([
    setupUser,
    testOne,
    validateAccount,
    sendEmail,
], function (err, result) {
    if (err) {
        return res.status(400).jsonp({error: err});
    }
});


});  
Jacobian answered 28/12, 2016 at 14:21 Comment(0)
H
6

You certainly can't use if statements inside an array, but I think what you're looking for is this:

async.waterfall([
    callOne,
    callTwo,
    function (condition, callback) {
        if (condition > 0) {
            async.waterfall([
                callTest1,
                callTest2
            ], callback);
        } else {
            async.waterfall([
                callTest3,
                callTest4
            ], callback);
        }
    },
    callThree,
    callFour,
    callFive,
], function (err, result) {
    if (err) {
        return res.status(400).jsonp({error: err});
    }
});
Hernadez answered 28/12, 2016 at 14:30 Comment(3)
You can replace function (results) { callback(results); } with callbackVole
wow.. thanks for the quick feedback. I will try that now.. Thanks Phil..Jacobian
@philipfwilson, sure, you can accept the answer if it solves your problemHernadez

© 2022 - 2024 — McMap. All rights reserved.