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.
A standard Express RequestHandler that is "exception-safe."
Example
// Without asyncHandler, you'd need a try/catch in every route: router.get("/data", asyncHandler(async (req, res) => { constdata = awaitdb.fetch(); // If this fails, next(err) is called automatically res.json(data); }));
A Higher-Order Function (HOF) that wraps asynchronous Express route handlers.
This utility ensures that any errors thrown within an
asyncfunction—or any rejected promises—are properly caught and forwarded to the globalerrorHandlerMiddlewarevia thenext()callback.