Node.js中For循环中的async.waterfall

时间:2015-08-04 01:14:41

标签: javascript node.js async.js

SELECT c.Tag, c.DateCreated AS most_recent, c.Comment, c.Author FROM ( SELECT Tag, MAX(DateCreated) AS MaxDate FROM Comments GROUP BY Tag ) AS md INNER JOIN Comments AS c ON c.Tag = md.Tag AND c.DateCreated = md.MaxDate INNER JOIN status AS s ON s.critera1 = c.criteria1 AND s.criteria2 = c.criteria2 循环中使用async.waterfall时,似乎Meteor.startup(function() { //do some logic... //... process.env.ROOT_URL = 'http://subdomain1.mydomain.com'; }); 循环在嵌套for完成其所有步骤之前迭代。如何避免这种情况?

for

输出

async.waterfall

所需输出

for(var i = 0; i < users.length; i++ ) {

    console.log(i)

    async.waterfall([
        function(callback) {
            callback(null, 'one', 'two');
        },
        function(arg1, arg2, callback) {
          // arg1 now equals 'one' and arg2 now equals 'two'
            callback(null, 'three');
        },
        function(arg1, callback) {
            // arg1 now equals 'three'
            callback(null, 'done');
        }
    ], function (err, result) {
        // result now equals 'done'
        console.log('done')
    });


}

2 个答案:

答案 0 :(得分:13)

您可以使用async的forEachLimit

var async = require("async")
var users = []; // Initialize user array or get it from DB

async.forEachLimit(users, 1, function(user, userCallback){

    async.waterfall([
        function(callback) {
            callback(null, 'one', 'two');
        },
        function(arg1, arg2, callback) {
            // arg1 now equals 'one' and arg2 now equals 'two'
            callback(null, 'three');
        },
        function(arg1, callback) {
            // arg1 now equals 'three'
            callback(null, 'done');
        }
    ], function (err, result) {
        // result now equals 'done'
        console.log('done')
        userCallback();
    });


}, function(err){
    console.log("User For Loop Completed");
});

答案 1 :(得分:-1)

您需要应用递归模式。我的建议是这样的

function foo(properties, callback){
/*Initialize inputs w/ useful defaults.  Using a properties object simply because it looks nicer, you can use individual values if you'd like.*/
        properties.start = properties.start || 0;
        properties.end = properties.end || 10; //or any default length
        properties.data = properties.data || [];

        async.waterfall([
          function(callback) {
        },
        function(arg1, arg2, callback) {
          /* arg1 now equals 'one' and arg2 now equals 'two' you can do something with that before continuing processing */
        },
        function(arg1, callback) {
            /* arg1 now equals 'three', you can do something with that before continuing processing */

        }
    ], function (err, result) {
        // result now equals 'done' 
        // now that we have fully processed one record, we need to either finish or recurse to the next element.  

     if(properties.index >= properties.end){
       callback();
     }
     else{
       properties.index++;
       foo(properties, callback);
     }

    })}

我已将回调传递给每个函数回调。如果你愿意,你可以选择以这种方式提前结束递归,或者你可以在你希望的那个地方做任何其他事情。这类似于我最近提出的一个问题:Patterns for asynchronous but sequential requests还有一些其他有趣的解决方案。