【问题标题】:nodejs and async.waterfall with if conditions and conditional function list.nodejs 和 async.waterfall 带有 if 条件和条件函数列表。
【发布时间】:2017-05-12 20:23:49
【问题描述】:

我一直在使用 async.waterfall 和 nodejs。它工作得很好,但现在我有一个关于流量的问题。

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

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

我只想测试一种情况..

如果条件为真

然后运行一些函数

否则

运行其他功能。

结束

清理

我也在尝试这个...一个 async.waterfall 调用两个 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});
    }
});


});  

【问题讨论】:

    标签: javascript node.js asynchronous async.js


    【解决方案1】:

    您当然不能在数组中使用 if 语句,但我认为您正在寻找的是:

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

    【讨论】:

    • 您可以将function (results) { callback(results); } 替换为callback
    • 哇.. 感谢您的快速反馈。我现在就试试……谢谢菲尔……
    • @philipfwilson,当然,如果它解决了您的问题,您可以接受答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    相关资源
    最近更新 更多