简体   繁体   English

Node.js async.series()不要等待回调完成

[英]Node.js async.series() don't wait callback to complete

My code works when mongoose.disconnect() is never called and node program has time to execute all callbacks. 我的代码在从不调用mongoose.disconnect()且节点程序有时间执行所有回调时起作用。 When mongoose.disconnect() is called at the end of the program and connection is lost those inserts to mongo naturally don't happen. 当在程序末尾调用mongoose.disconnect()且连接丢失时,自然不会发生向mongo的插入。

This async is not waiting for those inserts to complete. 此异步不等待这些插入完成。 Why, and how can I fix it? 为什么,如何解决?

mObjectList = array of objects ready for insert mObjectList =准备插入的对象数组

insertMongoObj: function(mObjectList, callback) {
  var tasks = [];

  for (i in mObjectList) {
    tasks.push(mObjectList[i].save());
  }

  async.parallel(tasks, function(err) {
    if (err) {
      callback(err, 0);
    }
  });
  callback(null, tasks.length);
}

Thank you for your fast reply! 感谢您的快速回复! Changed function as advised but same thing. 建议更改功能,但相同。 Working when connection is never closed and program is never exiting. 当连接永不关闭且程序永不退出时工作。 Mongoose.disconnect() is still closing connection before those inserts. Mongoose.disconnect()仍在那些插入之前关闭连接。

insertMongoObj : function(mObjectList, callback){

    var tasks = [];

    for (i in mObjectList) {
      tasks.push(mObjectList[i].save.bind(mObjectList[i]));
    }

    async.parallel(tasks, function(err) {
        if(err) return callback(err);
        winston.info("  file processing ready");
    });

    return callback();
}

The previous function calls are here: 以前的函数调用在这里:

mongoose.connection.once('open', function () {
    talendToMongo.processTalendExport(objPaths, function(){
        mongoose.disconnect();
    });

});

processTalendExport : function(talendEntityLocationList, callback){
    var mongoObjectList = [];
    var self = this;
    for(obj in talendEntityLocationList){
        winston.info("**Start processing directory:" + talendEntityLocationList[obj]);
        var fileList = fs.readdirSync(talendEntityLocationList[obj]);
        this.processEntityDir(talendEntityLocationList[obj], fileList, function(mongoObjectList){
            self.insertMongoObj(mongoObjectList, function(err){
                if(err) {
                    winston.error("  Error processing file:" + talendEntityLocationList[obj]);
                        process.exit(0);
                    } else {
                        winston.info(" Mongo Objects inserted");
                    }
                });
            });
        }
        callback();
    }

insertMongoObj : function(mObjectList, callback){
    var tasks = [];

    for (i in mObjectList) {
        tasks.push(mObjectList[i].save.bind(mObjectList[i]));
    }

    async.parallel(tasks, function(err) {
        if(err) return callback(err);
          winston.info("  file processing ready");
        });

        return callback();
    }

You need to move the last callback invocation into the callback for async.parallel : 您需要将最后一个回调调用移至async.parallel的回调中:

for (i in mObjectList) {
  tasks.push(mObjectList[i].save.bind(mObjectList[i]));
}

async.parallel(tasks, function(err) {
     if (err) {
         return callback(err, 0);
     }
     return callback(null, tasks.length);
 });

This is a great case where async.each is ideal. 这是一个很好的案例,其中async.each是理想的。

insertMongoObj: function(mObjectList, callback) {
    async.each(mObjectList, function(obj, done){
        obj.save(done);
    }, callback);
}

Update 更新

In processTalendExport , you have callback() after the for(obj in talendEntityLocationList){ loop instead of after the insertions are done, so it will call .disconnect before the insertions have had time to finish. processTalendExport ,您可以在for(obj in talendEntityLocationList){循环之后而不是在插入完成之后使用callback() ,因此它将在插入之前有时间调用.disconnect Depending on the client, and the network, that could always cause failures, or only fail sometimes. 根据客户端和网络的不同,这可能总是会导致故障,或者有时只会导致故障。 You should be using async.each(talendEntityLocationList, ... to look over that list too. 您也应该使用async.each(talendEntityLocationList, ...查看该列表。

Finally got this working correcting previous function callbacks and fixing async.eachSeries callbacks: 最后,完成了以下工作:更正先前的函数回调并修复async.eachSeries回调:

    insertMongoObjects : function(mongoObjList, callback){
        var insertCnt = 0;
        winston.info("    Start mongo object insert");
        async.eachSeries(mongoObjList,
            function(mongoObj, done){
                    mongoObj.save(function(save_result){
                        if(!save_result){ 
                            insertCnt++
                            done(null);
                        } else { done(save_result); }
                    });
            },
            function(err){
                if(err) {
                    winston.error("    Error inserting mongo object:");
                    winston.error(err);
                    callback(err);
                } else {
                    winston.info("    Mongo object insert successful for:" + insertCnt);
                    callback();
                }
            }
        );
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM