Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs and async.waterfall with if conditions and conditional function list.

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});
    }
});


});  
like image 229
philipfwilson Avatar asked Dec 28 '16 14:12

philipfwilson


1 Answers

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});
    }
});
like image 128
Max Koretskyi Avatar answered Oct 30 '22 02:10

Max Koretskyi