diff --git a/lib/helpers/each.js b/lib/helpers/each.js new file mode 100644 index 00000000000..fe7006931fd --- /dev/null +++ b/lib/helpers/each.js @@ -0,0 +1,25 @@ +'use strict'; + +module.exports = function each(arr, cb, done) { + if (arr.length === 0) { + return done(); + } + + let remaining = arr.length; + let err = null; + for (const v of arr) { + cb(v, function(_err) { + if (err != null) { + return; + } + if (_err != null) { + err = _err; + return done(err); + } + + if (--remaining <= 0) { + return done(); + } + }); + } +}; \ No newline at end of file diff --git a/lib/plugins/removeSubdocs.js b/lib/plugins/removeSubdocs.js index c7b2d24769a..44b2ea62790 100644 --- a/lib/plugins/removeSubdocs.js +++ b/lib/plugins/removeSubdocs.js @@ -1,6 +1,6 @@ 'use strict'; -const each = require('async/each'); +const each = require('../helpers/each'); /*! * ignore @@ -17,15 +17,8 @@ module.exports = function(schema) { const _this = this; const subdocs = this.$__getAllSubdocs(); - if (!subdocs.length) { - next(); - return; - } - each(subdocs, function(subdoc, cb) { - subdoc.$__remove(function(err) { - cb(err); - }); + subdoc.$__remove(cb); }, function(error) { if (error) { return _this.schema.s.hooks.execPost('remove:error', _this, [_this], { error: error }, function(error) { diff --git a/lib/plugins/saveSubdocs.js b/lib/plugins/saveSubdocs.js index dc27213683c..c0a3144e778 100644 --- a/lib/plugins/saveSubdocs.js +++ b/lib/plugins/saveSubdocs.js @@ -1,6 +1,6 @@ 'use strict'; -const each = require('async/each'); +const each = require('../helpers/each'); /*! * ignore diff --git a/test/document.test.js b/test/document.test.js index 2b27eae1d88..ba10cec8b26 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -7903,7 +7903,7 @@ describe('document', function() { } } }); - + const schema = new Schema({ items: [itemArray] });