node.js +异步系列执行

时间:2014-04-09 04:38:40

标签: javascript node.js asynchronous

在我的节点应用程序中,我使用async.series执行3个查询,执行得非常好..但在我的第3个查询执行中,我必须根据第3个查询的结果执行另一个查询,它也正常工作。但是我在第三个查询中执行了一些逻辑,所以在完成后我只需要调用callback ....

我的代码:

async.series({
    levels: function(cb) {
        sequelize.query("select country_id, country_name, level0, level1, level2, level3, level4 from levels").success(function(levelsResults) {
            levelsResult = levelsResults;
            cb(null, levelsResult);
        })
    },
    level1: function(cb) {
        sequelize.query("select id_0, name_0, name_1 from xxxxx group by id_0, name_0, name_1").success(function(level1Result) {
            level1result = level1Result;
            cb(null, level1result);
        })
    },
    keys: function(cb) {
        sequelize.query("select id_0 from xxxx group by id_0").success(function(id_0Result) {
            var obj = {};
            for (var i = 0; i < id_0Result.length; i++) {
                sequelize.query("select id_0, value->>'yyyy' as value from xxxx where id_0 = " + id_0Result[i].id_0 + " limit 1").success(function(keyResult) {
                    var keyArray = [];
                    var keysObjectArray = [];
                    id_0 = keyResult[0].id_0;
                    keyResult = keyResult[0].value;
                    keyResult = JSON.parse(keyResult);

                    for (var prop in keyResult) {
                        keyArray.push(prop)
                    }
                    obj["" + id_0 + ""] = keyArray;
                    keysObjectArray.push(obj);
                    cb(null, keysObjectArray);
                    ---.my problem is here cb is called in the 1st iteration of
                    for loop.It should be called after loop finishes

                })
            }
        })
    }
}, function seriesFinal(seriesErr, seriesResults) {
    if (seriesErr) throw new Error("Something bad!");
    onSuccess(JSON.stringify(seriesResults), callback);

});

我的结果是:

{"levels":[{levsls}],"level1":[{level1}],"keys":[{}]};

但我期待的是:

{"levels":[{levsls}],"level1":[{level1}],"keys":[{},{}]};

编辑:

我怎样才能使sequlize查询执行for循环,最后必须调用回调

帮我解决这个问题..提前谢谢......

2 个答案:

答案 0 :(得分:1)

“for”循环运行“n”个查询,这些查询随机回调“成功”函数。

您可以使用计数器确保所有查询都回调

sequelize.query("select id_0 from xxxx group by id_0").success(function(id_0Result){
                var obj = {};
                var counter = 0;
                for(var i=0; i<id_0Result.length; i++){
                    sequelize.query("select id_0, value->>'yyyy' as value from xxxx where id_0 = "+id_0Result[i].id_0+" limit 1").success(function(keyResult){
                            var keyArray=[];
                            var keysObjectArray =[];
                            id_0 = keyResult[0].id_0;                                
                            keyResult = keyResult[0].value;
                            keyResult = JSON.parse(keyResult);

                            for (var prop in keyResult){
                                keyArray.push(prop)
                            }                                
                            obj[""+id_0+""] = keyArray;                                
                            keysObjectArray.push(obj);   
                            counter  +=1;
                            if(counter === id_0Result.length )
                            {                             
                               cb(null, keysObjectArray);
                            }

                    })
                }

请注意,keysObjectArray中的顺序是随机的,回调也是如此。

希望它有所帮助。

的Yoann

答案 1 :(得分:1)

async还有async.series而不仅仅是async.forEach!您可以使用keys来执行循环,对于async.map函数,keys: function(cb) { sequelize.query("select id_0 from xxxx group by id_0").success(function(id_0Result){ var obj = {}; // Loop over each object in id_0Result, // and transform it into a new array async.map( id_0Result, function(result, cb) { sequelize.query("select id_0, value->>'yyyy' as value from xxxx where id_0 = "+result.id_0+" limit 1").success(function(keyResult){ var keyArray=[]; id_0 = keyResult[0].id_0; keyResult = keyResult[0].value; keyResult = JSON.parse(keyResult); for (var prop in keyResult){ keyArray.push(prop) } obj[""+id_0+""] = keyArray; // Add this object to the array result for async.map return cb(null, obj); }) }, // Call the callback for async.series with the result of // the mapping; effectively cb(null, transformedArray) cb ); }); } 可以很好地执行:

{{1}}