Node.js 为什么我的闭包没有在每次循环迭代中及时执行?

Node.js 为什么我的闭包没有在每次循环迭代中及时执行?,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,车站附近的公交车以空数组开始。在对数据库的异步调用中,应该填充它。然后在调用完成后,我想使用其中的数据。当我运行这段代码时,总线的最终控制台日志在内部数据库调用之前执行,即使我在闭包中有循环调用。根据《邮报》的说法,关闭应该是有效的,但在这里它对我来说毫无用处 var buses_near_stops = []; buses_near_stops.test = "TEST"; // return these fields of each location document in the da

车站附近的公交车以空数组开始。在对数据库的异步调用中,应该填充它。然后在调用完成后,我想使用其中的数据。当我运行这段代码时,总线的最终控制台日志在内部数据库调用之前执行,即使我在闭包中有循环调用。根据《邮报》的说法,关闭应该是有效的,但在这里它对我来说毫无用处

var buses_near_stops = [];
buses_near_stops.test = "TEST";


// return these fields of each location document in the database
Location.find({}, 'service_name coordinates vehicle_id last_gps_fix', function(err, doc) {
    //console.log('location found ' + JSON.stringify(doc));
    if(err){return next(err);}

         doc.forEach(function(j,k) {
         //Find a stop that is near enough to each given bus that we can say the bus is 'at' that stop
        //Making sure it returns 1 stop now because I don't know proper distance
             (function(buses_near_stops) {
                   Stop.findOne({coordinates: { $near : j.coordinates, $maxDistance: .0001}
              }, function(err, stop){
                    if(err){return next(err);}

                        console.log('stop found ' + j.service_name + " " + JSON.stringify(stop));
                        // service_name is null if bus is out of service (I believe)
                        if(stop !== null && j.service_name !== null) {
                            var service_name_of_bus = j.service_name;
                            console.log('service name of bus ' + service_name_of_bus);

                            // Find the service document associated with service_name_of_bus
                            var service_of_name = Service.findOne({name: service_name_of_bus}, function(err, service_of_name){
                                if(err){return next(err);}

                                // If the service has 'stop' on its route
                                if(service_of_name != null && service_of_name.routes[0].stops.indexOf(stop.stop_id) > -1) {
                                    console.log('stop found on service');
                                    // We have now found a bus that is stopped at a stop on its route
                                    console.log('test ' + buses_near_stops.test);
                                    buses_near_stops.push(
                                        {
                                            time: j.last_gps_fix,
                                            bus_coords: j.coordinates,
                                            stop_coords: stop.coordinates,
                                            vehicle_id: j.vehicle_id,
                                            stop_id: stop.stop_id,
                                            service_name: service_name_of_bus
                                        });

                                    console.log('length ' + buses_near_stops.length);
                                }
                            });

                        }
                    })}(buses_near_stops));

                });
                console.log('buses near stops ' + JSON.stringify(buses_near_stops));
            });

因为doc.forEach是同步函数,而Stop.findOne是异步函数是的,我知道这一点。我必须在同步循环中有一个异步函数。这就是我将异步函数放在闭包中的原因。这是一种试图让一切都按顺序执行的尝试。@michaelAdam如果你想让异步代码按顺序执行,你需要使用类似的东西,而不是forEach。在另一个问题中,闭包用于在每次迭代中正确捕获ans的值,它对流控制没有任何影响。因此异步库只是每个人都使用的标准方法?@michaelAdam Yep,这可能是最流行的方法。