title: Function execution order in JavaScript with NodeJS
body: <p>I have a MySQL database and I'm working with NodeJS to getting access to it.
  Here is the code that I'm using to make an INSERT in my table.</p>
  
  <pre><code>var mysql = require('mysql');
  var fs = require('fs');
  var readline = require('readline');
  
  var ID_User = {value: 0};
  
  var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "blah",
    multipleStatements: true
  });
  
  function insertNewUser(name_user, password_user) {
      con.connect(function(err) {
          if (err) throw err;
  
          console.log("Connected!");
  
          var sql_user = "INSERT INTO `blah`.`User` (`Name`, `Password`) VALUES ('" + name_user + "', '" + password_user + "');"
  
          con.query(sql_user, function (err, result) {
              if (err) throw err;
              ID_User.value = result.insertId;
              console.log(ID_User);
          });
      });
  }
  </code></pre>
  
  <p>And this is the code that I'm using to execute this function:</p>
  
  <pre><code>console.log("user " + ID_User.value + " " + ID_User);
  insertNewUser("user", "123");
  console.log("some message");
  console.log("user " + ID_User.value + " " + ID_User);
  </code></pre>
  
  <p>Everything is in the same file, but when I execute the code, this is my output:</p>
  
  <p><a href="https://i.stack.imgur.com/lpDv5.png" rel="nofollow noreferrer">the output isn't in order</a></p>
  
  <p>Why it happens this? How I can control the order of execution of this functions?
  I tried to put make a second function but this one (<em>insertNewUser()</em>) always is executed until the end. </p>
  
  <p>Thanks beforehand.</p>