title: Node.js async.series() don't wait callback to complete
body: <p>My code works when <code>mongoose.disconnect()</code> is never called and node program has time to execute all callbacks. When <code>mongoose.disconnect()</code> is called at the end of the program and connection is lost those inserts to mongo naturally don't happen.</p>
  
  <p>This async is not waiting for those inserts to complete. Why, and how can I fix it?</p>
  
  <p>mObjectList = array of objects ready for insert</p>
  
  <pre class="lang-js prettyprint-override"><code>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);
  }
  </code></pre>
  
  <p>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.</p>
  
  <pre class="lang-js prettyprint-override"><code>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();
  }
  </code></pre>
  
  <hr>
  
  <p>The previous function calls are here:
  </p>
  
  <pre><code>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();
      }
  </code></pre>