"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.asyncRetryTransactionMiddleware = exports.asyncMiddleware = void 0;
const tslib_1 = require("tslib");
const bluebird_1 = tslib_1.__importDefault(require("bluebird"));
const database_utils_1 = require("../helpers/database-utils");
function asyncMiddleware(fun) {
    return (req, res, next) => {
        if (Array.isArray(fun) === true) {
            return bluebird_1.default.each(fun, 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(req, res, next))
            .catch(err => next(err));
    };
}
exports.asyncMiddleware = asyncMiddleware;
function asyncRetryTransactionMiddleware(fun) {
    return (req, res, next) => {
        return Promise.resolve((0, database_utils_1.retryTransactionWrapper)(fun, req, res, next)).catch(err => next(err));
    };
}
exports.asyncRetryTransactionMiddleware = asyncRetryTransactionMiddleware;
//# sourceMappingURL=async.js.map