Use async/await in controllers

This commit is contained in:
Chocobozzz 2017-10-25 11:55:06 +02:00
parent 5f04dd2f74
commit eb08047657
No known key found for this signature in database
GPG key ID: 583A612D890159BE
20 changed files with 823 additions and 992 deletions

View file

@ -0,0 +1,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
}