title: Use control flow on async method with node.js and async.js
body: <p>I'm trying to change a working code that doesn't have any control flow to display when everything is done how much change were done. WIthout control flow, it's not possible.</p>
  
  <p>To do so, I'm trying async, but I'm a bit confused here.</p>
  
  <p>Old code:</p>
  
  <pre><code>public static populate(){
      // Read all files inside the populate folder.
      fs.readdir(__config.path.populate, function(err, files){
          if(err){
              consoleDev(err);
          }else{
              // For each file, get the data to populate stored in the file.
              _.each(files, function(file){
                  _.each(require(__config.path.base + __config.path.populate + file), function(documents){
                      // The file name must be the same as the targeted model.
                      var Model = __Dao.getModel(file.split('.')[0]);// Filter the extension.
  
                      if(Model){
                          // For each data, try to add it if it does not exist in the DB.
                          Model.findOrCreate(documents, function(err, doc, created){
                              if(created){
                                  consoleDev('The following document was created in the [' + Model.modelName + '] collection: ' + JSON.stringify(doc));
                              }
                          });
                      }else{
                          consoleDev('Unable to populate the file: ' + file + '. The model was not found.')
                      }
                  });
              });
          }
      });
  }
  </code></pre>
  
  <p>New code:</p>
  
  <pre><code>public static populate(){
      // Read all files inside the populate folder.
      fs.readdir(__config.path.populate, function(err, files){
          if(err){
              consoleDev(err);
          }else{
              _.each(files, function(file){
                  // Read all files in async mode with control flow.
                  async.each(require(__config.path.base + __config.path.populate + file), function(documents, callback){
                      // The file name must be the same as the targeted model.
                      var Model = __Dao.getModel(file.split('.')[0]);// Filter the extension.
  
                      if(Model){
                          // For each data, try to add it if it does not exist in the DB.
                          Model.findOrCreate(documents, function(err, doc, created){
                              if(created){
                                  consoleDev('The following document was created in the [' + Model.modelName + '] collection: ' + JSON.stringify(doc));
                                  Populate._added++;
                              }else{
                                  Populate._read++;
                              }
                              callback();
                          });
                      }else{
                          callback('Unable to populate the file: ' + file + '. The model was not found.')
                      }
                  });
              }, function(err){
                  if(err){
                      console.log(err);
                  }
                  consoleDev(Populate._added + ' documents were added. There are now ' + Populate._read + ' + ' + Populate._added + ' = ' + (Populate._read + Populate._added) + ' documents populated.');
              });
          }
      });
  }
  </code></pre>
  
  <p>Nothing is showed on the console at the end and I don't have any error on the console. The script still works and add documents that wasn't present in the DB before. But I still don't have a message saying me how much were added.</p>
  
  <p>I tried to do <code>async.each(files, function(file, callback){</code> instead but I get the error <code>Error: Callback was already called.</code>.</p>
  
  <p>What am I missing here?</p>