diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-10-25 11:55:06 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-10-26 09:11:38 +0200 |
commit | eb08047657e739bcd9e592d76307befa3998482b (patch) | |
tree | fc309f51ece792fd4307c4af510710a853e1d6b2 /server/middlewares | |
parent | 5f04dd2f743961e0a06c29531cc3ccc9e4928d56 (diff) | |
download | PeerTube-eb08047657e739bcd9e592d76307befa3998482b.tar.gz PeerTube-eb08047657e739bcd9e592d76307befa3998482b.tar.zst PeerTube-eb08047657e739bcd9e592d76307befa3998482b.zip |
Use async/await in controllers
Diffstat (limited to 'server/middlewares')
-rw-r--r-- | server/middlewares/async.ts | 16 | ||||
-rw-r--r-- | server/middlewares/index.ts | 1 |
2 files changed, 17 insertions, 0 deletions
diff --git a/server/middlewares/async.ts b/server/middlewares/async.ts new file mode 100644 index 000000000..29ebd169d --- /dev/null +++ b/server/middlewares/async.ts | |||
@@ -0,0 +1,16 @@ | |||
1 | import { 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 | ||
5 | function 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 | |||
14 | export { | ||
15 | asyncMiddleware | ||
16 | } | ||
diff --git a/server/middlewares/index.ts b/server/middlewares/index.ts index d71dd2452..0e2c850e1 100644 --- a/server/middlewares/index.ts +++ b/server/middlewares/index.ts | |||
@@ -1,5 +1,6 @@ | |||
1 | export * from './validators' | 1 | export * from './validators' |
2 | export * from './admin' | 2 | export * from './admin' |
3 | export * from './async' | ||
3 | export * from './oauth' | 4 | export * from './oauth' |
4 | export * from './pagination' | 5 | export * from './pagination' |
5 | export * from './pods' | 6 | export * from './pods' |