title: node js to return global variable set in callback function
body: <p>I have 2 functions in <code>javascript</code> and I want to set a global variable in a callback and then return it from my main function.  I know javascript is async by nature so what would be the best way to do this? I have the code below:</p>
  
  <pre><code>var results="";
  
  function getData() {
  
  var sql = require("mssql");
  var dbConfig={
          server:"server",
          database: "db",
          user:"user",
          password: "pw"
  }
  
  
  
  
          var conn = new sql.Connection(dbConfig);
          var req = new sql.Request(conn);
          conn.connect(function (err){
                  if (err) {
                  console.log(err);
                  return;
                  }
  
  
                  req.query("SELECT * FROM table",resultsCallback)
  
                  conn.close();
          });
  
      return results;
  }
  
  function resultsCallback (err, recordset) {
  
          var tableify = require('tableify');
  
          if (err) {
                  console.log(err);
          }
          else {
  
                  var html = tableify(recordset);
                  html = html.replace('&lt;table&gt;','');
                  html = html.replace('&lt;/table&gt;',');
                  results=html;
  
          }
  };
  </code></pre>