Server - Technical Reference - v26.4.1
    Preparing search index...

    Function asyncHandler

    • A Higher-Order Function (HOF) that wraps asynchronous Express route handlers.

      This utility ensures that any errors thrown within an async function—or any rejected promises—are properly caught and forwarded to the global errorHandlerMiddleware via the next() callback.

      Parameters

      • requestHandlerFn: RequestHandler

        The asynchronous Express middleware or controller function.

      Returns (req: Request, res: Response, next: NextFunction) => void

      A standard Express RequestHandler that is "exception-safe."

      // Without asyncHandler, you'd need a try/catch in every route:
      router.get("/data", asyncHandler(async (req, res) => {
      const data = await db.fetch(); // If this fails, next(err) is called automatically
      res.json(data);
      }));