diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 14:44:51 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:53 +0100 |
commit | 4e50b6a1c9a3eb261e04ede73241648e6edf21d6 (patch) | |
tree | e1c6c121d561ffc1cf2996daec03a1e7f27f0a25 /server/middlewares/async.ts | |
parent | 74bb2cb8348d6794ed3a0e2ec94c8c9abdde82cf (diff) | |
download | PeerTube-4e50b6a1c9a3eb261e04ede73241648e6edf21d6.tar.gz PeerTube-4e50b6a1c9a3eb261e04ede73241648e6edf21d6.tar.zst PeerTube-4e50b6a1c9a3eb261e04ede73241648e6edf21d6.zip |
Add shares forward and collection on videos/video channels
Diffstat (limited to 'server/middlewares/async.ts')
-rw-r--r-- | server/middlewares/async.ts | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/server/middlewares/async.ts b/server/middlewares/async.ts index 29ebd169d..9692f9be7 100644 --- a/server/middlewares/async.ts +++ b/server/middlewares/async.ts | |||
@@ -1,10 +1,18 @@ | |||
1 | import { Request, Response, NextFunction } from 'express' | 1 | import { Request, Response, NextFunction, RequestHandler } from 'express' |
2 | import { eachSeries } from 'async' | ||
2 | 3 | ||
3 | // Syntactic sugar to avoid try/catch in express controllers | 4 | // Syntactic sugar to avoid try/catch in express controllers |
4 | // Thanks: https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016 | 5 | // Thanks: https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016 |
5 | function asyncMiddleware (fn: (req: Request, res: Response, next: NextFunction) => Promise<any>) { | 6 | function asyncMiddleware (fun: RequestHandler | RequestHandler[]) { |
6 | return (req: Request, res: Response, next: NextFunction) => { | 7 | return (req: Request, res: Response, next: NextFunction) => { |
7 | return Promise.resolve(fn(req, res, next)) | 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)) | ||
8 | .catch(next) | 16 | .catch(next) |
9 | } | 17 | } |
10 | } | 18 | } |