nodejs和async.waterfall使用if条件和条件函数列表。 [英] nodejs and async.waterfall with if conditions and conditional function list.

查看:112
本文介绍了nodejs和async.waterfall使用if条件和条件函数列表。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用async.waterfall和nodejs。它工作得很好,但现在我对流量有疑问。

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

我想在async.waterfall流程中使用简单的if条件。

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 ..

如果条件为真

然后运行一些函数

else

运行其他功能。

endif

清理

我也在尝试这个...一个async.waterfall调用两个async.waterfall / s

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


});  


推荐答案

你当然不能使用如果数组中的语句,但我认为你要找的是:

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

这篇关于nodejs和async.waterfall使用if条件和条件函数列表。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆