View as Json

Async Handler

A lightweight async error-handling wrapper for Express route handlers.

Installation Guide

npx servercn-cli@latest add async-handler

Usage Example

Wrap any async route handler with AsyncHandler so rejected promises are forwarded to your Express error-handling middleware.

import { AsyncHandler } from "@/utils/async-handler";
import { Request, Response, NextFunction } from "express";
import { AsyncHandler } from "../utils/async-handler";
 
export const registerUser = AsyncHandler(
  async (req: Request, res: Response, next: NextFunction) => {
    /*
     * business logic
     * your actual code
     */
  }
);

File & Folder Structure

Loading files...
npx servercn-cli@latest add async-handler

Contributed by