Bluebird在nodejs中承诺

时间:2015-03-24 09:42:37

标签: javascript node.js mongodb express mongoskin

我在nodejs中有一系列的承诺。它们看起来像这样:

 codecommon.db.executeDbCommandAsync({
                'aggregate':'table1', 
                pipeline:[
                    {$match:{uid:uid}},
                    {$match :{timestamp:{$gt:start_week,$lt:end_week}}},
                    {$group:{_id:{"pageSession":"$pageSession"}, totalpersession:{$sum:1}, min:{$min:"$timestamp"}, max:{$max:"$timestamp"}}},
                    {$match:{totalpersession:{$gt:1}}},
                    {$project:{seconds:{$subtract:["$max","$min"]}}}
                ]
            })  .then(
    function(result) {
        dayspv = get_days(result[0].documents[0].result);
        totaldays = sum_days(dayspv,totaldays);         //vb interaction
        return common.db.executeDbCommandAsync({
                'aggregate':'table2', 
                pipeline:[
                    {$match:{uid:uid}},
                    {$match :{timestamp:{$gt:start_week,$lt:end_week}}},
                    {$group:{_id:{"pageSession":"$pageSession"}, totalpersession:{$sum:1}, min:{$min:"$timestamp"}, max:{$max:"$timestamp"}}},
                    {$match:{totalpersession:{$gt:1}}},
                    {$project:{seconds:{$subtract:["$max","$min"]}}}
                ]
            });     }) . then(      function(result) {
            days = get_days(result[0].documents[0].result);
            totaldays = sum_days(days,totaldays);
            //sm interaction            return common.db.executeDbCommandAsync({
                'aggregate':'table3', 
                pipeline:[
                    {$match:{uid:uid}},
                    {$match :{timestamp:{$gt:start_week,$lt:end_week}}},
                    {$group:{_id:{"session":"$session"}, totalpersession:{$sum:1}, min:{$min:"$timestamp"}, max:{$max:"$timestamp"}}},
                    {$match:{totalpersession:{$gt:1}}},
                    {$project:{seconds:{$subtract:["$max","$min"]}}}
                ]
            });

                }) .then( ...etc

考虑到在每个承诺中我都有相同的mongo查询。我想做一个这样的函数:

var check_interaction = function(table, uid, start_week, end_week){

    return common.db.executeDbCommandAsync({
                'aggregate':'\'' + table + '\'', 
                pipeline:[
                    {$match:{uid:uid}},
                    {$match :{timestamp:{$gt:start_week,$lt:end_week}}},
                    {$group:{_id:{"session" :"$session" }, totalpersession:{$sum:1}, min:{$min:"$timestamp"}, max:{$max:"$timestamp"}}},
                    {$match:{totalpersession:{$gt:1}}},
                    {$project:{seconds:{$subtract:["$max","$min"]}}}
                ]
            });

};

并将承诺链切换为类似的东西:

check_interaction(...) .then( process some data
                             return another check_interaction(...);)
                       .then( process some data
                             return another check_interaction(...);)
                       .then(...etc

所有的mongoskin函数/查询都是有效的,它不会抛出错误,通过整个链(用控制台日志调试),你没有提供从一个到另一个的数据。有人能帮助我吗?

0 个答案:

没有答案