diff options
Diffstat (limited to 'server/middlewares/async.ts')
-rw-r--r-- | server/middlewares/async.ts | 16 |
1 files changed, 16 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 | } | ||