1 import { eachSeries } from 'async'
2 import { NextFunction, Request, RequestHandler, Response } from 'express'
4 // Syntactic sugar to avoid try/catch in express controllers
5 // Thanks: https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016
7 export type RequestPromiseHandler = (req: Request, res: Response, next: NextFunction) => Promise<any>
9 function asyncMiddleware (fun: RequestPromiseHandler | RequestPromiseHandler[]) {
10 return (req: Request, res: Response, next: NextFunction) => {
11 if (Array.isArray(fun) === true) {
12 return eachSeries(fun as RequestHandler[], (f, cb) => {
13 Promise.resolve(f(req, res, cb))
18 return Promise.resolve((fun as RequestHandler)(req, res, next))
23 // ---------------------------------------------------------------------------