javascript - Node.js async.series() don't wait callback to complete -
my code works when mongoose.disconnect()
never called , node program has time execute callbacks. when mongoose.disconnect()
called @ end of program , connection lost inserts mongo naturally don't happen.
this async not waiting inserts complete. why, , how can fix it?
mobjectlist = array of objects ready insert
insertmongoobj: function(mobjectlist, callback) { var tasks = []; (i in mobjectlist) { tasks.push(mobjectlist[i].save()); } async.parallel(tasks, function(err) { if (err) { callback(err, 0); } }); callback(null, tasks.length); }
thank fast reply! changed function advised same thing. working when connection never closed , program never exiting. mongoose.disconnect() still closing connection before inserts.
insertmongoobj : function(mobjectlist, callback){ var tasks = []; (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 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 = []; (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 move last callback invocation callback 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); });
Comments
Post a Comment