myApp.registerEndpoint('GET', '/api/firstUser', async function(req, res) { try { let firstUser = await getFirstUser(); res.json(firstUser) } catch (err) { console.error(err); res.status(500); } });
Understand promises before you start using async/await
Daniel Brain

Perhaps it’s just me, but I find this syntax sort of cumbersome. It looks like an if/else statement. What about using .catch chained on the end of the awaited promise?

So what you have here would look like this instead:

myApp.registerEndpoint('GET', '/api/firstUser', async function(req, res) {
let firstUser = await getFirstUser().catch(err => {

This way your code isn’t chained together in the old promise style, but you kind of neatly attach the error catcher to the end of the function. This feels cleaner to me, and works exactly the same way.

Anyway, great article! Really helpful stuff.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.