title: Is the connect method in the NPM mysql module blocking?
body: <p>The official mysql NPM package documentation for Node.js (<a href="https://www.npmjs.com/package/mysql" rel="nofollow noreferrer">here</a>) makes the following suggestion for connecting to a database and executing a basic query:</p>
  
  <pre><code>var mysql      = require('mysql');
  var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'me',
    password : 'secret',
    database : 'my_db'
  });
  
  connection.connect();
  
  connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
    if (error) throw error;
    console.log('The solution is: ', results[0].solution);
  });
  
  connection.end();
  </code></pre>
  
  <p>The connect method also takes an optional callback function, and the documentation provides the following example of that usage (ignore the comments in the example below; this example is actually trying to demonstrate something else):</p>
  
  <pre><code>var connection = require('mysql').createConnection({
    port: 84943, // WRONG PORT 
  });
  
  connection.connect(function(err) {
    console.log(err.code); // 'ECONNREFUSED' 
    console.log(err.fatal); // true 
  });
  
  connection.query('SELECT 1', function (error, results, fields) {
    console.log(error.code); // 'ECONNREFUSED' 
    console.log(error.fatal); // true 
  });
  </code></pre>
  
  <p>In both cases, the query method is called after the connect method, and <em>not</em>, as might be expected, within the callback of the connect method.  This seems to imply that the connect method is blocking (ie/ synchronous).</p>
  
  <p>So I have a simple question, followed by some more nuanced questions:</p>
  
  <p><strong>Is the <code>connect</code> method in the NPM mysql module blocking?</strong></p>
  
  <p><strong>If so, doesn't this go against the Node convention?</strong> Wouldn't the convention be  to do a query within the callback passed to the <code>connect</code> method?</p>
  
  <p><strong>Finally, if the connect method is blocking and does go against Node convention, why did the designers of the mysql module decide to make <code>connect</code> blocking? What are the advantages of this approach?</strong> As the most popular mysql Node.js package, I'm sure there was a reason this approach was taken.</p>