title: How to wait for function to finish before continuning in Node.js
body: <p>I am trying to create a route in Node.js/Express that reads data from two queries and then  increments a count based on that data from the queires. Since Node.js is asynchronous my total is displayed before all the data has been read. </p>
  
  <p>I created a simple example that gets to the point of what I am currently doing</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  
  
  var total = 0;
  
  /* GET home page. */
  router.get('/', function(req, res, next) {
    increment(3);
    increment(2);
    console.log(total);
    res.end();
  });
  
  
  
  var increment = function(n){
      //Wait for n seconds before incrementing total n times
      setTimeout(function(){
      for(i = 0; i &lt; n; i++){
          total++;
      }   
      }, n *1000);
  };
  module.exports = router;
  </code></pre>
  
  <p>I'm not sure what I would have to do in order to wait until both functions finish before I print the total. Would I have to create a custom Event Emitter to achieve this?</p>