aboutsummaryrefslogblamecommitdiffhomepage
path: root/server/middlewares/async.ts
blob: 29ebd169d4a3e911adc2f86a22226394fcc4068b (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16















                                                                                                  
import { Request, Response, NextFunction } from 'express'

// Syntactic sugar to avoid try/catch in express controllers
// Thanks: https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016
function asyncMiddleware (fn: (req: Request, res: Response, next: NextFunction) => Promise<any>) {
  return (req: Request, res: Response, next: NextFunction) => {
    return Promise.resolve(fn(req, res, next))
      .catch(next)
  }
}

// ---------------------------------------------------------------------------

export {
  asyncMiddleware
}