title: Express JS page rendered before async function was executed
body: <p>I have a problem with express.js routing. I want to get all restaurants when users open /restaurant/map. Problem is that function gm.geocode is async so page is rendered before that function is executed. How would I get variabile mapsData at the right time, so when page /restaurant/map is loaded? What is the right way to do that? </p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var gm = require('googlemaps');
  var fs=require('fs');
  var staticDB=require('./staticDB.js');
  var mapsData=[];
  
  
  /* GET home page. */
  router.get('/', function (req, res, next) {
  //not important
  });
  
  router.get('/map/', function (req, res, next) {
      var counter=0;
      console.log('Request URL:', req.originalUrl);
      for (i = 0; i &lt; staticDB.addresses.addresses.length; i++) {
          gm.geocode(staticDB.addresses.addresses[i].street + ", " + staticDB.addresses.addresses[i].city, function (err, data){
              mapsData.push({
                  "name": staticDB.restaurants.restaurants[counter].name,
                  "location": data.results[0].geometry.location
              });
              counter++;
          }, false);
      }
      res.render('map', {
          title: 'title',
          restaurants: mapsData
      });
  });
  
  module.exports = router;
  </code></pre>