nodejs和async.Waterfall具有IF条件和条件功能列表



我一直在使用async.waterfall和nodejs。它运行良好,但现在我有一个关于流的疑问。

我想在异步流中使用简单的条件。

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

我只想测试一个条件。

如果条件为true

然后运行一些功能

其他

运行其他功能。

endif

清理

我也在尝试...一个异步。

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

});  

您当然不能在数组中使用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});
    }
});

最新更新