Skip to content

Instantly share code, notes, and snippets.

@betabites
betabites / asyncEndpoint.ts
Last active June 10, 2024 04:40
A wrapper function for async express.js middleware/route handlers.
import {NextFunction} from "express";
export function AsyncEndpoint(originalMethod: (req: Request, res: Response, next: NextFunction) => PromiseLike<any>) {
async function replacementMethod(req: Request, res: Response, next: NextFunction) {
try {
await originalMethod(req, res, next)
next()
} catch (e) {
next(e)
}