]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/async.ts
Update readme, architecture
[github/Chocobozzz/PeerTube.git] / server / middlewares / async.ts
CommitLineData
4e50b6a1
C
1import { Request, Response, NextFunction, RequestHandler } from 'express'
2import { eachSeries } from 'async'
eb080476
C
3
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
4e50b6a1 6function asyncMiddleware (fun: RequestHandler | RequestHandler[]) {
eb080476 7 return (req: Request, res: Response, next: NextFunction) => {
4e50b6a1
C
8 if (Array.isArray(fun) === true) {
9 return eachSeries(fun as RequestHandler[], (f, cb) => {
10 Promise.resolve(f(req, res, cb))
11 .catch(next)
12 }, next)
13 }
14
15 return Promise.resolve((fun as RequestHandler)(req, res, next))
eb080476
C
16 .catch(next)
17 }
18}
19
20// ---------------------------------------------------------------------------
21
22export {
23 asyncMiddleware
24}