0

Here is a sample of the working async code without mongodb. The problem is, if i replace the vars (data1_nodb,...) with the db.collection.find(); function, all needed db vars received at the end and the for()-loop ends not correct. Hope someone can help. OA

    var calc = new Array();
    function mach1(callback){               

                        error_buy = 0;                                                  

                        // some vars                            

                        for(var x_c99 = 0; x_c99 < array_temp_check0.length;x_c99++){

                            // some vars                        

                                calc[x_c99] = new Array();                                      
                                calc[x_c99][0]= new Array();    

                                calc[x_c99][0][0] = "dummy1";
                                calc[x_c99][0][1] = "dummy2";
                                calc[x_c99][0][2] = "dummy3";
                                calc[x_c99][0][3] = "dummy4";
                                calc[x_c99][0][4] = "dummy5";

                                function start_query(callback) {

                                        data1_nodb = "data1";
                                        data2_nodb = "data2";
                                        data3_nodb = "data3";
                                        data4_nodb = "data4";


                                        calc[x_c99][0][0] = data1_nodb;
                                        calc[x_c99][0][1] = data2_nodb;
                                        calc[x_c99][0][2] = data3_nodb;


                                        callback(data1_nodb,data2_nodb,etc..);

                                }

                                        start_query(function() {

                                            console.log("start_query OK!"); 

                                            function start_query2(callback) {

                                            data4_nodb = "data5";
                                            data5_nodb = "data6";
                                            data6_nodb = "data7";


                                            calc[x_c99][0][3] = data4_nodb;
                                            calc[x_c99][0][4] = data5_nodb;

                                            callback(data5_nodb,data6_nodb,etc..);

                                        }                                           

                                        start_query2(function() {

                                            console.log("start_query2 OK!");    

                                                function start_query3(callback) {


                                                    for(...){

                                                            // do something
                                                        }

                                                        callback(vars...);                                              
                                                    }

                                            start_query3(function() {
                                                console.log("start_query3 OK!");    
                                            });

                                        });
                                    }); 
                            }

                        callback(calc); 

                };


                function mach2(callback){

                    mach1(function() {

                        console.log("mach1 OK!");

                        for(...){                                                                                               
                        // do something                                                         
                        }   

                    });         

                    callback(calc,error_buy);

                };                          

                mach2(function() {
                console.log("mach2 OK 2!");                                         
                });
4

1 回答 1

0

您需要使用该collection.find()方法的异步性质并等待所有这些都完成。一种非常流行的方法是使用异步模块。该模块允许您运行多个并行任务并等待它们完成其async.parallel()方法:

async.parallel([
  function (callback) {
    db.foo.find({}, callback);
  },
  function (callback) {
    db.bar.find({}, callback);
  },
  function (callback) {
    db.baz.find({}, callback);
  }
], function (err, results) {
  // results[0] is the result of the first query, etc
});
于 2012-08-04T23:44:18.340 回答