title: Why does the first program work but not the second?
body: <pre><code>var mysql = require('mysql')
  
  var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'user',
    password : 'pass',
    database : 'mydb'
  })
  
  var values = [ ['Dave', 20],
                      ['Judd', 22],
                      ['Snek', 24],
                      ['Dale', 26] 
              ];
  
  connection.connect(function(err) {
    if (err) {
      return console.error('error: ' + err.message);
    }
  
    let createTodos = `create table if not exists siblings(
                            id int primary key auto_increment,
                            name varchar(255)not null,
                            age tinyint(2) not null default 0
                        )`;
  
    connection.query(createTodos, function(err, results, fields) {
      if (err) {
        console.log(err.message);
      }
    });
  
    var sql = 'insert into siblings (name, age) values ?';
  connection.query(sql, [values], function (err, result) {
      if (err) throw err;
      console.log("Number of records inserted: " + result.affectedRows);
    });
  });
  </code></pre>
  
  <p>This works perfectly fine and does what I intended, creates the table with column for name and age if the table does not already exist, then inserts the values in the value array accordingly.</p>
  
  <p>This does not work</p>
  
  <pre><code>var mysql = require('mysql')
  
  var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'user',
    password : 'pass',
    database : 'mydb'
  })
  
  var values = [ ['Dave', 20],
                      ['Judd', 22],
                      ['Snek', 24],
                      ['Dale', 26] 
              ];
  
  connection.connect(function(err) {
    if (err) {
      return console.error('error: ' + err.message);
    }
  
    let createTodos = `create table if not exists siblings(
                            id int primary key auto_increment,
                            name varchar(255)not null,
                            age tinyint(2) not null default 0
                        )`;
  
    connection.query(createTodos, function(err, results, fields) {
      if (err) {
        console.log(err.message);
      }
    });
  
  });
  
  var sql = 'insert into siblings (name, age) values ?';
      connection.query(sql, [values], function (err, result) {
          if (err) throw err;
          console.log("Number of records inserted: " + result.affectedRows);
        });
  </code></pre>
  
  <p>The difference is that the query to insert the values array is outside of the connect function call. I understand that this must have something to do with the callback functions being passed to connect and them perhaps being async functions, but can someone correct me or elaborate further on this? The control flow of node is eluding me.</p>