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

℡╲_俬逩灬. 提交于 2019-12-21 04:51:03

问题


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


});  

回答1:


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


来源:https://stackoverflow.com/questions/41363965/nodejs-and-async-waterfall-with-if-conditions-and-conditional-function-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!