title: Sync function on nodejs to MySQL
body: <p>I have a check script with a MySQL query, the problem is that the authorization function is executed before it was done MySQL query so it returns always false. How can fix it?</p>
  
  <pre><code> mysql= require('mysql');
   var connection = mysql.createConnection({});
  
  
   check_auth(req.query.s){
   var sql = "query to mysql";
      connection.query(sql, function(err, results) {
          if (err) throw err;
          if (results.length  &gt; 0) {
              return true;
          }else{
              return false;
          } 
  }
  
  io.configure(function () {
  io.set('authorization', function(req, callback) {
  var auth=check_auth(req.query.s);
  console.log(auth);
  if (auth===true){
      return callback(null, true);
  }else{
  return callback('notauth',false);
  }
  });
  });
  </code></pre>
  
  <p>EDIT: new structure as Jarema but still don't work</p>
  
  <pre><code> mysql= require('mysql');
   var connection = mysql.createConnection({});
  
  
  function check_auth(input, callback){
   var sql = "query to mysql";
      connection.query(sql, function(err, results) {
          if (err) callback(err);
          if (results.length  &gt; 0) {
              callback(null,results.values);
          }else{
              callback(null, false);
          } 
  }
  
  io.configure(function () {
      io.set('authorization', function(req, callback) {
          check_auth(req.query.s, function(err, result) {
              if (err) {
                  //handle that error somehow. we dont not structure of Your func.
                  return console.log('error:(');
  
              }
              if(result === false) {
                  return callback('notauth', false);
              } else {
                  return callback(null, result);;
              }
          });
      });
  });
  </code></pre>
  
  <p>error is</p>
  
  <pre><code> TypeError: undefined is not a function
  at Query._callback (myserver.js:54:12)
  at Query.Sequence.end (node_modules/mysql/lib/protocol/sequences/Sequence.js:78:24)
  at Query._handleFinalResultPacket (node_modules/mysql/lib/protocol/sequences/Query.js:143:8)
  at Query.EofPacket (node_modules/mysql/lib/protocol/sequences/Query.js:127:8)
  at Protocol._parsePacket (node_modules/mysql/lib/protocol/Protocol.js:205:24)
  at Parser.write (node_modules/mysql/lib/protocol/Parser.js:62:12)
  at Protocol.write (node_modules/mysql/lib/protocol/Protocol.js:37:16)
  at Socket.&lt;anonymous&gt; (node_modules/mysql/lib/Connection.js:73:28)
  at Socket.EventEmitter.emit (events.js:95:17)
  at Socket.&lt;anonymous&gt; (_stream_readable.js:746:14)
  </code></pre>
  
  <p>line 54 is</p>
  
  <pre><code> callback(null,results.values);
  </code></pre>