异步系列nodejs中的异步foreach

async foreach inside async series nodejs

我正在研究 节点异步库。我无法按我想要的 sequence 执行。我不知道我哪里错了 这是代码..在评论中我定义了订单号.. 目前它按 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.In 你的场景试试这个方法:

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。 尝试 co-foreach-series 获取每个数组元素并逐个执行异步函数。

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();
})

})