import { eachSeries } from 'async' ; import type { NextFunction , Request , RequestHandler , Response } from 'express' ; export type RequestPromiseHandler = ( ( req : Request , res : Response , next : NextFunction ) => Promise < any > ) ; function asyncMiddleware ( fun ) { return ( req , res , next ) => { if ( Array . isArray ( fun ) ) { eachSeries ( fun as RequestHandler [ ] , ( f , cb ) => { Promise . resolve ( f ( req , res , ( err ) => cb ( err ) ) ) . catch ( err => next ( err ) ) ; } , next ) ; return ; } Promise . resolve ( ( fun as RequestHandler ) ( req , res , next ) ) . catch ( err => next ( err ) ) ; } ; } export { asyncMiddleware } ;