import { ErrorRequestHandler , RequestHandler } from 'express' ; /**
 * Handles promises in routes.
 */ function asyncHandler ( handler ) ; function asyncHandler ( handler ) ; function asyncHandler ( handler ) { if ( handler . length === 2 || handler . length === 3 ) { const scoped = ( req , res , next ) => Promise . resolve ( ( handler as RequestHandler ) ( req , res , next ) ) . catch ( next ) ; return scoped ; } else if ( handler . length === 4 ) { const scoped = ( err , req , res , next ) => Promise . resolve ( ( handler as ErrorRequestHandler ) ( err , req , res , next ) ) . catch ( next ) ; return scoped ; } else { throw new Error ( `Failed to asyncHandle() function "${ handler . name }"` ) ; } } export default asyncHandler ;