0

I am a long-time old school JS engineer, struggling with Promises. We are upgrading to HAPI v17, and I am having trouble figuring out how to convert the existing code to make it work. Here's the problem (generally):

I have a HAPI v16 route that does something like:

  server.route({
        method: 'GET',
        path: '/product/prd-{productId}/{filename*}',
        handler: function (request, reply) {
          var productId = encodeURIComponent(request.params.productId);
          if( /*someCondition*/ ){
              server.inject('/staticmessages/product', function (SCResponse) {
                  if (SCResponse.statusCode === 200 && SCResponse.statusMessage === 'OK') {
                      productStaticContent = JSON.parse(SCResponse.payload).messages;
                  }
                  PDPController.renderPDP(request, reply, productId, productStaticContent);
              });
          }
          else {
              PDPController.renderPDP(request, reply, productId, productStaticContent);
          }
        }
    });

Basically, the route handler checks some internal flags to determine if it needs to make an asynchronous call to retrieve some strings or not, then forwards control to a method on a controller object to fulfill the request.

So, I have a number of issues: First, is the conditional async call. Second, the server.inject call is now an await call (as of HAPI v17). Third, I need to return a Promise from the handler (whether I make the async call or not). Fourth, I guess the controller method has to be a promise?

I have reviewed (studied!) probably a dozen websites covering Promises, and a number of videos, and I guess it's really a paradigm shift, as I'm really not getting the full picture. Seems like it should be simple, but there's some unidentified hurdle I'm just not getting over. Any help would be much appreciated!

user1588877
  • 775
  • 1
  • 5
  • 9
  • Does the documentation indicate that it is beneficial to return a value (promise or otherwise) from the 'handler'? – Roamer-1888 Dec 06 '18 at 01:56
  • It is quite possible that promisification holds no benefit for you here. – Roamer-1888 Dec 06 '18 at 11:36
  • It's not about benefit. HAPI v17 requires the handler to return a promise. – user1588877 Dec 10 '18 at 21:09
  • Can you link to documentation that says that please? I have looked and can't find any examples of a route handler returning anything other than string or JS plain object. – Roamer-1888 Dec 10 '18 at 22:10
  • Here's a few: https://github.com/hapijs/hapi/issues/3658, https://hapijs.com/api, https://futurestud.io/tutorials/hapi-v17-upgrade-guide-your-move-to-async-await, https://youtu.be/8uYtExM_jTI – user1588877 Dec 10 '18 at 23:28
  • I've not read every word, but looks OK. Good luck with it. – Roamer-1888 Dec 11 '18 at 13:11

1 Answers1

1

I'll write them using async/await as

server.route({
    method: 'GET',
    path: '/product/prd-{productId}/{filename*}',
    handler: async function (request, reply) { // put async
      var productId = encodeURIComponent(request.params.productId);
      const condition = await checkCondition(); // put await 

      if (condition) {
        const SCResponse = await server.inject('/staticmessages/product'); // put await
        if (SCResponse.statusCode === 200 && SCResponse.statusMessage === 'OK') {
          productStaticContent = JSON.parse(SCResponse.payload).messages;
        }
        return PDPController.renderPDP(request, reply, productId, productStaticContent); // add return
      }

      return PDPController.renderPDP(request, reply, productId, productStaticContent);      
    }
});

Fourth, I guess the controller method has to be a promise?

Not necessary, we use async for handler so it will always return promise.

Hope it helps

deerawan
  • 8,002
  • 5
  • 42
  • 51
  • Isn't async just an implied promise? The suggestion worked for me. Thanks! I'll leave the issue open in case there is a better proposal. There are a number of other code changes needed to clean up our code, so this may eventually be reduced to a handler with (request, h) params to take care of the conditional async operation within it. But this solution makes it work for the interim. – user1588877 Dec 07 '18 at 00:30