Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
54 lines (39 sloc) 2.84 KB

Express is the most popular HTTP server framework for Node.js, but unfortunately it doesn't have good support for async/await. Express doesn't handle errors in async functions. Fastify is an alternative Node.js server framework that enjoys much better support for async/await in addition to better performance. In this article, I'll show you how Fastify works with async/await and show you the corner cases you need to be aware of.

Hello, World with Async/Await and Fastify

Like Express, the "Hello, World" case works well with async/await. Below is an example Fastify server that waits for about 100ms before responding with "Hello, World!".

[require:fastify.*basic]

Where Fastify beats Express is handling errors in async route handlers. If you throw an error in an async route handler in Express, Express will never send a response. Fastify actually handles errors in async functions for you and reports them as HTTP 500 Internal Server Errors.

[require:fastify.*basic error]

Edge Cases

Once you call the reply.send() function, any subsequent errors in your async function will not be reported.

[require:fastify.*error after reply]

If you're using Fastify with async/await, you should return data from the function body instead calling reply.send() to prevent potential silent errors. After you return from an async function, the async function body can't throw an error.

[require:fastify.*return]

Moving On

In addition to having better performance on benchmarks, Fastify has better support for async/await than Express. Failure to handle async errors is a common problem for JavaScript frameworks, so I'm impressed that Fastify handles them correctly. Give Fastify a shot if you're looking for a server framework that supports async/await out of the box.

Want to learn how to identify whether your favorite npm modules work with async/await without resorting to Google or Stack Overflow? Chapter 4 of my new ebook, Mastering Async/Await, explains the basic principles for determining whether frameworks like React and Socket.IO support async/await. Get your copy!