]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/async.ts
Use async/await in controllers
[github/Chocobozzz/PeerTube.git] / server / middlewares / async.ts
CommitLineData
eb080476
C
1import { Request, Response, NextFunction } from 'express'
2
3// Syntactic sugar to avoid try/catch in express controllers
4// Thanks: https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016
5function asyncMiddleware (fn: (req: Request, res: Response, next: NextFunction) => Promise<any>) {
6 return (req: Request, res: Response, next: NextFunction) => {
7 return Promise.resolve(fn(req, res, next))
8 .catch(next)
9 }
10}
11
12// ---------------------------------------------------------------------------
13
14export {
15 asyncMiddleware
16}