title: How to return a promise from a Hapi handler
body: <p>I have a very simple Hapi server that returns a Promise that when resolved calls reply(), I understand that this is possible from its documentation: <a href="https://hapijs.com/api#route-handler" rel="nofollow noreferrer">https://hapijs.com/api#route-handler</a></p>
  
  <p>The problem is that the Promise seems to not get resolved (and consequently I don't receive a response) until I refresh the page a couple of times or after a really long time. The promise returned by check_ips works as expected so it seems to be something related to how Hapi handles it.</p>
  
  <pre><code>const Hapi = require('hapi');
  const ping = require('net-ping')
  
  const server = new Hapi.Server({ debug: { request: ['error'] } });
  const port = process.env.PORT || 8080;
  server.connection({ port: port, host: 'localhost' });
  
  const session = ping.createSession();
  
  check_ips = function(ips) {
      var pings = []
  
      for (var i = 0; i &lt; ips.length; i++) {
          pings.push(new Promise(function(fulfill, reject) {
              session.pingHost(ips[i], function(err, target)  {
                  if (err) {
                      if (err instanceof ping.RequestTimedOutError)
                          fulfill(false)
                      else
                          reject(err)
                  } else {
                      fulfill(true)
                  }
              })
          }))
      }
  
      return Promise.all(pings).then(function(results) {
          response = {}
          for (var i = 0; i &lt; ips.length; i++) {
              response[ips[i]] = results[i]
          }
  
          return(JSON.stringify(response))
      })
  }
  
  server.route({
      method: 'GET',
      path: '/check_ips/{ips}',
      handler: function(req, reply) {
          if (!req.params["ips"]) {
              return reply({"error": "No IPs received"})
          }
  
          ips = req.params["ips"].split(",")
          return check_ips(ips).then(reply)
      }
  });                                                                                                                         
  
  server.start(function(err) {
      if (err) {
          throw err;
      }
      console.log(`Server running at: ${server.info.uri}`);
  });
  </code></pre>