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!".
Where Fastify beats Express is handling errors in async route handlers. If
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.
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.
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!