Skip to content

Instantly share code, notes, and snippets.

@Katamori
Last active October 9, 2019 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Katamori/2175e3e896747f78f7863036fa4a03e4 to your computer and use it in GitHub Desktop.
Save Katamori/2175e3e896747f78f7863036fa4a03e4 to your computer and use it in GitHub Desktop.
Using async/await in HapiJS route handler
/*
* 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