Skip to content

Commit 92d6ca5

Browse files
committed
refactor: make saveSubdocsPreSave async
1 parent 59e9381 commit 92d6ca5

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

lib/plugins/saveSubdocs.js

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11
'use strict';
22

3-
const each = require('../helpers/each');
4-
53
/*!
64
* ignore
75
*/
86

97
module.exports = function saveSubdocs(schema) {
108
const unshift = true;
11-
schema.s.hooks.pre('save', false, function saveSubdocsPreSave(next) {
9+
schema.s.hooks.pre('save', false, async function saveSubdocsPreSave() {
1210
if (this.$isSubdocument) {
13-
next();
1411
return;
1512
}
1613

1714
const _this = this;
1815
const subdocs = this.$getAllSubdocs({ useCache: true });
1916

2017
if (!subdocs.length) {
21-
next();
2218
return;
2319
}
2420

25-
each(subdocs, function(subdoc, cb) {
26-
subdoc.$__schema.s.hooks.execPre('save', subdoc, function(err) {
27-
cb(err);
21+
await Promise.all(subdocs.map(async (subdoc) => {
22+
return new Promise((resolve, reject) => {
23+
subdoc.$__schema.s.hooks.execPre('save', subdoc, function(err) {
24+
if (err) reject(err);
25+
else resolve();
26+
});
2827
});
29-
}, function(error) {
30-
// Invalidate subdocs cache because subdoc pre hooks can add new subdocuments
31-
if (_this.$__.saveOptions) {
32-
_this.$__.saveOptions.__subdocs = null;
33-
}
34-
if (error) {
35-
return next(error);
36-
}
37-
next();
38-
});
28+
}));
29+
30+
// Invalidate subdocs cache because subdoc pre hooks can add new subdocuments
31+
if (_this.$__.saveOptions) {
32+
_this.$__.saveOptions.__subdocs = null;
33+
}
3934
}, null, unshift);
4035

4136
schema.s.hooks.post('save', async function saveSubdocsPostDeleteOne() {

0 commit comments

Comments
 (0)