title: nodejs res.end not working in callback
body: <p>I'm trying to retrieve data from a database and send it back to the user, but since mysql queries work asynchronously, I can't just put the code that sends the response after the query code, I have to send the response within the callback function of the query.</p>
  
  <pre><code>var express = require('express');
  var fs = require('fs');
  var mysql = require('mysql')
  var app = express();
  var con = mysql.createConnection({
      host:"localhost",
      user:"root",
      password:"",
      database:"tempdb"
  })
  app.use(function (req, res, next) {
      res.setHeader('Access-Control-Allow-Origin', 'http://localhost');
      res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, 
      PATCH, DELETE');
      res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-
      type');
      res.setHeader('Access-Control-Allow-Credentials', true);
      next();
  });
  app.get('/',function(req,res,next){
      function foo(res){
          con.query('SELECT * FROM `data` WHERE 1',function(err,result){
              if (err) throw err;
              res.end('weee',function(err){
                  if (err) throw err;
              )}
          })
      }
      foo(res)
      next();
  }) 
  app.listen(1001)
  </code></pre>
  
  <p>Even with something as simple as this, when the data from the database isn't processed or used at all, the res.end function doesn't send any data. </p>
  
  <p>I've tried many variations of this, using waterfall callbacks, putting the res.end in a function outside of the query and calling it, but nothing seems to work. The only time it works it when I put it outside the query, but since the query is async I can't get any data out of it that way. Is there a way to work around this or am I just missing something?</p>
  
  <p>Thanks in advance!</p>