Last active
October 9, 2019 00:25
-
-
Save Katamori/2175e3e896747f78f7863036fa4a03e4 to your computer and use it in GitHub Desktop.
Using async/await in HapiJS route handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Sharing it here because it took me a while to find out. | |
* If you need to use asynchronous operations in the router handlers specified in HapiJS (via `server.route()`), | |
* the methods shown in the tutorial won't help. The handler method itself has to be specified like this: | |
*/ | |
{ | |
method: 'GET', | |
path: '/', | |
handler: async function (request, h) { | |
// Find all users | |
let data = functionReturningPromise().then(functionOutput => { | |
// do something with functionOutput | |
return newOutput; | |
}); | |
return await data; | |
} | |
}, | |
/* | |
* You can use this object as an input in `server.route()` | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment