title: Nodejs mysql asynchronous calling (waiting for the DB query to be over)
body: <p>I have an issue I want my code to complete the db query and then proceed to next commands, how do I make it happen, the only solutions I found was using promise, async and await. I could not find any way to integrate them. Please use this code and show me the necessary changes and where I should incorporate them.</p>
  
  <p>The code is as follows:</p>
  
  <pre><code>const mysql = require('mysql')
  const bodyparser = require('body-parser');
  
  console.log("in the db1")
  
    let gh="select * from table1"
    var connection = mysql.createConnection({
    host: 'xxxxxxxxxx',
    user: 'root',
    password: 'root',
    database: 'db1'
    })
  
    connection.query(gh, function (error, results, fields) {
      console.log('Connection Successful')
      connection.end();
    })
  console.log("in the db2")
  
  </code></pre>
  
  <p>The output which I am getting is :</p>
  
  <pre><code>in the db1
  in the db2
  Connection Successful
  
  </code></pre>
  
  <p>I just want the code to wait for the DB query to be completed and then proceed to next step so i can in future manipulate DB query results accordingly.</p>
  
  <p>Thanks in advance</p>