asynchronous系列nodejs中的asynchronousforeach

我正在处理节点asynchronous库 。 我无法按照我想要的顺序执行。 我不知道我要去哪里错了这里是代码..在评论中我已经定义了订单号..目前它的执行2,3,4,5,1顺序我想在1,2,3,4 ,5号 ….好心帮忙

function getAsExhibitors(req, res) { //getting all exhibitors against an event var exhibitors = []; var eac_app_names = []; async.series([function(callback){ models.EacExhibitorsExt.find({ deleted: false,userid: req.user._id}).sort({ modified: -1 }).exec(function(err, myExhibitors) { exhibitors = myExhibitors; callback(); }); },function(callback){ async.forEach(exhibitors,function(exhibitor,callback){ models.Eac.findById(exhibitor.eventid).exec(function(err,eac){ eac_app_names[exhibitors.indexOf(exhibitor)]=eac; console.log("-----------------1--------------"+eac_app_names); }); console.log("-----------------2--------------"+eac_app_names); callback(); },function(err) { console.log("-----------------3--------------"+eac_app_names); callback(); }); }],function(err) { //This function gets called after the two tasks have called their "task callbacks" if (err) return next(err); //Here locals will be populated with 'exhibitors' and 'apps' console.log("-------------------------4------"+eac_app_names); console.log("-------------------------5------"+eac_app_names.name); res.locals.exhibitors = exhibitors; res.locals.eac_app_names = eac_app_names; res.render('eac/eac_reg_as_exhibitor', { title: "My Event Exhibitors", asexhibitor: exhibitors,app_names:eac_app_names}); }); }; 

所有mongoose方法工作asynchronous。在你的情况下尝试这种方式:

 function getAsExhibitors(req, res) { //getting all exhibitors against an event var exhibitors = []; var eac_app_names = []; async.series([function(callback){ models.EacExhibitorsExt.find({ deleted: false,userid: req.user._id}).sort({ modified: -1 }).exec(function(err, myExhibitors) { exhibitors = myExhibitors; callback(); }); },function(callback){ async.forEach(exhibitors,function(exhibitor,callback){ models.Eac.findById(exhibitor.eventid).exec(function(err,eac){ eac_app_names[exhibitors.indexOf(exhibitor)]=eac; console.log("-----------------1--------------"+eac_app_names); console.log("-----------------2--------------"+eac_app_names); callback(); }); },function(err) { console.log("-----------------3--------------"+eac_app_names); callback(); }); }],function(err) { //This function gets called after the two tasks have called their "task callbacks" if (err) return next(err); //Here locals will be populated with 'exhibitors' and 'apps' console.log("-------------------------4------"+eac_app_names); console.log("-------------------------5------"+eac_app_names.name); res.locals.exhibitors = exhibitors; res.locals.eac_app_names = eac_app_names; res.render('eac/eac_reg_as_exhibitor', { title: "My Event Exhibitors", asexhibitor: exhibitors,app_names:eac_app_names}); }); }; 

欢迎您使用es6与发电机。 尝试使用联合foreach-series来获取每个数组元素并逐个执行asynchronous函数。

ForEach系列示例

 foreach(yourArray, function(element, index) { // Each of this function will be executed one after one co(function*() { // Do some async task, and wait until this task be finished yield yourAsyncFunc(); yield doOtherAsyncTasks(); }) 

})