Back to all snippets
Library/Node.js/Async Route Handler
typescriptintermediateexpressasyncutilities

How to implement Async Route Handler in Typescript

Wrapper for async Express route handlers

Quick Answer

`asyncHandler` wraps an async Express route so any rejected promise is automatically forwarded to `next(err)` — no try/catch boilerplate in every route.

Code Snippet

1import { Request, Response, NextFunction } from 'express';
2
3type AsyncHandler = (
4  req: Request,
5  res: Response,
6  next: NextFunction
7) => Promise<any>;
8
9const asyncHandler = (fn: AsyncHandler) => {
10  return (req: Request, res: Response, next: NextFunction) => {
11    Promise.resolve(fn(req, res, next)).catch(next);
12  };
13};
14
15export default asyncHandler;

What is Async Route Handler?

Express does not natively catch errors thrown in async route handlers — unhandled promise rejections will crash the process or hang the request. asyncHandler wraps an async function and passes any rejection to Express's next(err) so your error middleware handles it cleanly.

How It Works

  1. 1The wrapper calls the original handler and wraps the returned Promise with `.catch(next)`.
  2. 2Any thrown error or rejected promise is forwarded to Express's `next(err)` automatically.
  3. 3Your error middleware then handles it as it would any other error.
  4. 4Express 5 handles async errors natively, but this is needed for Express 4.

Common Use Cases

  • Database queries - Async route that queries a DB without try/catch boilerplate
  • Third-party API calls - Async fetch calls that may reject
  • File operations - Async file reads in route handlers
  • Any async Express route - Drop-in wrapper to avoid repetitive try/catch blocks

Key Benefits

  • Eliminates repetitive try/catch in every async route
  • Integrates seamlessly with Express error middleware
  • Keeps route handler code clean and focused on business logic
  • Works with Express 4 and 5

Common Mistakes to Avoid

  • Forgetting to wrap the handler — if you pass an un-wrapped async function Express 4 will not catch its rejections.
  • Using try/catch inside an asyncHandler-wrapped route — redundant and hides errors from the centralized handler.
  • Not registering an error middleware — asyncHandler forwards to `next(err)` but something must handle it.

Quick Tips

  • Click the "Copy" button above to copy the code to your clipboard
  • This code is production-ready and can be used in your projects immediately
  • Check out related snippets below for more typescript examples

Frequently Asked Questions

Is asyncHandler still needed in Express 5?

No — Express 5 natively catches rejected promises from async route handlers. This pattern is only required for Express 4.

About This Typescript Code Snippet

This free typescript code snippet for async route handler is production-ready and copy-paste friendly. Whether you are building a web app, API, or frontend interface, this intermediate-level example will help you implement async route handler quickly and correctly.

All snippets in the Snippetly library follow typescript best practices and are tested for real-world use. You can adapt this code to work with React, Vue, Node.js, or any project that uses typescript.

Tags: express, async, utilities  | Language: typescript  | Difficulty: intermediate  | Category: Node.js

Build Your Own Snippet Library

Organise your team's code snippets with Snippetly. Share knowledge and boost productivity across your organisation.