title: Nodejs module export, wait for function to finish
body: <p>I am developping an app using node js
  in my export_db.js where I export the connection and query function</p>
  
  <p>When I call the function, inside the export_db the output is really a string filled with information, but in my main.js the output is undefined, as if the function wasn't finished and the code continue to run before the results comes in.</p>
  
  <p>How can I force it to wait ?</p>
  
  <p>File export_db.js</p>
  
  <pre><code>var con = mysql.createConnection({
  ....
      });
  
  con.connect(function(err) {if (err) throw err;});
  
  con.CustomQuery = function(SQLQuery){
      ..DO Stuff
      return stringoutput="";
      con.query(SQLQuery, function (err, result,fields) {            
          if (err) throw err;                  
          var arr =  result;          
          //Do Stuff Transform result into a String
          stringoutput=result as string
          });
      return string output;
  
  });
  
   module.exports = con;    
  </code></pre>
  
  <p>File import_db.js</p>
  
  <pre><code>var db = require('../db/export_db_connection');
  //DO Stuff
  Queryresult = db.CustomQuery(SQLQuery);
  bot.reply(Queryresult) // Send the String to the bot to get a response message
  //DO Stuffs
  </code></pre>