title: Getting output of mysql queries synchronously in node.js
body: <p>I have the following code:</p>
  
  <pre><code>app.get('/showtable1', (req, res) =&gt; {
    console.log("yo ho");
    mysqlconnection.query('SELECT d_name FROM datacenters', (err, rows, fields) =&gt; {
      if (!err) {
        var array = [];
        var tier = ['Class 0','Class 1','Class 2','Class 3'];
        var str = '';
        Object.keys(rows).forEach(function(key) {
          var row = rows[key];
          array.push(row.d_name);
        });
        for( var i = 0 ; i&lt;array.length ; i++) {
          str += '&lt;tr&gt;&lt;td&gt;'+array[i]+'&lt;/td&gt;';
          console.log('sfsf');
          for (var j=0 ; j&lt; tier.length ; j++) {
            let query1  = "SELECT * FROM `datacenters` WHERE  d_name = '"+array[i]+"' AND d_tiers LIKE '%"+tier[j]+"%'"
            mysqlconnection.query(query1,(err,rows) =&gt; {
              if(!err) {
                if(rows.length !=0) {
                  console.log(1);
                } else {
                  console.log(0);
                }
              }
            });
          }
        }
        res.send(rows);
      } 
      else
        console.log(err);
    })
  });
  </code></pre>
  
  <p>The output of this code is:</p>
  
  <pre><code>yo ho
  sfsf
  sfsf
  sfsf
  1
  1
  0
  0
  1
  1
  1
  0
  1
  1
  1
  0
  </code></pre>
  
  <p>But I need the output as:</p>
  
  <pre><code>yo ho
  sfsf
  1
  1
  0
  0
  sfsf
  1
  1
  1
  0
  sfsf
  1
  1
  1
  0
  </code></pre>
  
  <p>The first output is coming because the code is running asynchronously; but I need to run it synchronously. How can I do this?</p>