title: Node & MySQL: Can't end connection -> Async confusion
body: <p>I'm trying to write a Node program that populates my MySQL database with data from files I have on disk. I may or may not be going about this the right way, but it's working. What I'm having trouble with is understanding how I should be handling allowing asynchronous functions to finish before the connection to the DB is ended. Ultimately, I'll be reading lots of data files, and insert them into the database like I did below. I could just use <code>readFileSync</code> instead of the asynchronous version, but I need to get a better handle on asynchronous functions.</p>
  
  <p>When I insert the wine categories below, it works fine since it's not using an asynchronous function. However, when I use <code>readFile</code> to get data from a file, I get an error that connection ended before any of the queries were executed:</p>
  
  <pre><code>connection.connect( function(err) {
      if(err) {
          console.log(err);
      }
  });
  
  // Take a table and the values, and insert a new row into a table
  function insert_into( table, values ) {
      if( values instanceof Array ) {
          values = values.map( function( value ) {
              return '"' + value + '"';
          }).join(', ');
      } else {
          values = '"' + values + '"';
      }
  
      var statement = 'INSERT INTO ' + table + ' VALUES (NULL, ' + values + ')';
      connection.query( statement, function(err, rows, fields) {
        if (err) throw err;
  
        console.log( values + " successfully added.");
      });
  };
  
  // Populate the wine_categories table
  var wine_categories = [
      'red', 'white', 'rose', 'sparkling', 'fortified'
  ];
  
  // Works fine when used alone
  wine_categories.forEach( function( element ) {
      insert_into( 'wine_categories', element );
  });
  
  // Populate the countries table
  // connection.end() runs before this finishes its job
  fs.readFile( countries, 'utf8', function (err, data) {
      if (err) {
          throw err;
      } else {
          var codes = Array.prototype.map.call( 
              data.split('\n'), function( country ) {
                  return country.split('\t');
          });
  
          codes.forEach( function( country ) {
              if( country[1].length &gt; 25 ) {
                  country[1] = country[1].substring(0, 25);
              }
              insert_into( 'countries', country );
          });
      }
  }); 
  
  connection.end();
  </code></pre>
  
  <p>Obviously, <code>connection.end()</code> needs to happen after all of the inserts have completed, but I'm not sure how to handle that. I don't want it to be a callback for the <code>readFile</code> call because I'll ultimately have many of similar calls in this file.</p>
  
  <p>How should I structure my code so that all of the queries execute and <code>connection.end()</code> runs when they're all finished? The answer is probably obvious to an asynchronous wiz...</p>