Skip to main content

asyncHandler

function asyncHandler(fn: function)

Middleware to handle async route errors by passing them to Express error handler.

Returns: function

  • Express middleware function that catches errors and passes them to next().
PARAMETERTYPEOPTIONALDEFAULTDESCRIPTION
fnfunctionThe async route handler function.
const express = require("express");

app.get("/example", asyncHandler(async (req, res) => {
const data = await someAsyncOperation();
res.json(data);
}));