Skip to content

Commit

Permalink
refactor: remove async.parallel() calls
Browse files Browse the repository at this point in the history
Re: #8073
  • Loading branch information
vkarpov15 committed Aug 18, 2019
1 parent 5595ead commit 20fa775
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
17 changes: 14 additions & 3 deletions lib/helpers/updateValidators.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -223,6 +234,6 @@ module.exports = function(query, schema, castedDoc, options) {
return callback(err);
}
callback(null);
});
}
};
};
17 changes: 15 additions & 2 deletions lib/model.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
};
Expand Down

0 comments on commit 20fa775

Please sign in to comment.