From 20fa775bc55b326b7a77283c44115deef1393f67 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sun, 18 Aug 2019 10:25:06 -0400 Subject: [PATCH] refactor: remove async.parallel() calls Re: #8073 --- lib/helpers/updateValidators.js | 17 ++++++++++++++--- lib/model.js | 17 +++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/helpers/updateValidators.js b/lib/helpers/updateValidators.js index 4eeecbb2c5e..f58f905f321 100644 --- a/lib/helpers/updateValidators.js +++ b/lib/helpers/updateValidators.js @@ -9,7 +9,6 @@ const ValidationError = require('../error/validation'); const cleanPositionalOperators = require('./schema/cleanPositionalOperators'); const flatten = require('./common').flatten; const modifiedPaths = require('./common').modifiedPaths; -const parallel = require('async/parallel'); /** * Applies validators and defaults to update and findOneAndUpdate operations, @@ -214,7 +213,19 @@ module.exports = function(query, schema, castedDoc, options) { } return function(callback) { - parallel(validatorsToExecute, function() { + let numValidators = validatorsToExecute.length; + if (numValidators === 0) { + return _done(); + } + for (const validator of validatorsToExecute) { + validator(function() { + if (--numValidators <= 0) { + _done(); + } + }); + } + + function _done() { if (validationErrors.length) { const err = new ValidationError(null); for (let i = 0; i < validationErrors.length; ++i) { @@ -223,6 +234,6 @@ module.exports = function(query, schema, castedDoc, options) { return callback(err); } callback(null); - }); + } }; }; diff --git a/lib/model.js b/lib/model.js index 533e1abc951..af1a1e81241 100644 --- a/lib/model.js +++ b/lib/model.js @@ -37,7 +37,6 @@ const isPathSelectedInclusive = require('./helpers/projection/isPathSelectedIncl const get = require('./helpers/get'); const leanPopulateMap = require('./helpers/populate/leanPopulateMap'); const modifiedPaths = require('./helpers/update/modifiedPaths'); -const parallel = require('async/parallel'); const parallelLimit = require('async/parallelLimit'); const util = require('util'); const utils = require('./utils'); @@ -3035,7 +3034,11 @@ Model.create = function create(doc, options, callback) { }); }); - parallel(toExecute, (error, res) => { + let numFns = toExecute.length; + if (numFns === 0) { + return cb(null, []); + } + const _done = (error, res) => { const savedDocs = []; const len = res.length; for (let i = 0; i < len; ++i) { @@ -3053,6 +3056,16 @@ Model.create = function create(doc, options, callback) { } else { cb.apply(this, [null].concat(savedDocs)); } + }; + + let _res = []; + toExecute.forEach((fn, i) => { + fn((err, res) => { + _res[i] = res; + if (--numFns <= 0) { + return _done(null, _res); + } + }); }); }, this.events); };