title: Terminate mysql connection after multiple queries have executed
body: <p>I have some node.js code which fetches data from an API in a loop and runs mutliple mysql queries to update some rows.</p>
  
  <p>The issue I have is that the script keeps running until I terminate the mysql connection with <code>connection.end()</code>. I am a newbie in asynchronous code. Where do I call the termination function so that it executes when all the queries have finished executing? What's the right design pattern for this? Would <a href="https://www.npmjs.com/package/async-waterfall" rel="nofollow noreferrer">waterfall</a> be any good?</p>
  
  <p>This is a snippet from the code I have at the moment (error handling removed for simplicity):</p>
  
  <pre><code>var connection = mysql.createConnection({ host, user, etc... });
  
  for (var i = 0; i &lt; 10; i++) {
      var url = "http://api.com?i="+i;
  
      request(url, function(error, response, body) {
  
          var data = JSON.parse(body);
  
          for (el in data) {
              connection.query(
                  "UPDATE table SET col = ? WHERE symbol = ?",
                  [
                      data[el].col,
                      el
                  ]
              );
          }
      });
  }
  
  // this will run before all queries have executed
  // resulting in an error
  connection.end(); 
  </code></pre>