From 7a9e420a02434e4f16c99e7d58da9075dff25d15 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 2 Aug 2022 14:41:44 +0200 Subject: Remove uneeded async --- server/middlewares/async.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'server/middlewares/async.ts') diff --git a/server/middlewares/async.ts b/server/middlewares/async.ts index 9d0193536..7e131257d 100644 --- a/server/middlewares/async.ts +++ b/server/middlewares/async.ts @@ -1,21 +1,26 @@ -import { eachSeries } from 'async' +import Bluebird from 'bluebird' import { NextFunction, Request, RequestHandler, Response } from 'express' import { ValidationChain } from 'express-validator' import { ExpressPromiseHandler } from '@server/types/express-handler' import { retryTransactionWrapper } from '../helpers/database-utils' -// Syntactic sugar to avoid try/catch in express controllers -// Thanks: https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016 +// Syntactic sugar to avoid try/catch in express controllers/middlewares export type RequestPromiseHandler = ValidationChain | ExpressPromiseHandler function asyncMiddleware (fun: RequestPromiseHandler | RequestPromiseHandler[]) { return (req: Request, res: Response, next: NextFunction) => { if (Array.isArray(fun) === true) { - return eachSeries(fun as RequestHandler[], (f, cb) => { - Promise.resolve(f(req, res, err => cb(err))) - .catch(err => next(err)) - }, next) + return Bluebird.each(fun as RequestPromiseHandler[], f => { + return new Promise((resolve, reject) => { + return asyncMiddleware(f)(req, res, err => { + if (err) return reject(err) + + return resolve() + }) + }) + }).then(() => next()) + .catch(err => next(err)) } return Promise.resolve((fun as RequestHandler)(req, res, next)) -- cgit v1.2.3