File tree 1 file changed +13
-18
lines changed
1 file changed +13
-18
lines changed Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
- const each = require ( '../helpers/each' ) ;
4
-
5
3
/*!
6
4
* ignore
7
5
*/
8
6
9
7
module . exports = function saveSubdocs ( schema ) {
10
8
const unshift = true ;
11
- schema . s . hooks . pre ( 'save' , false , function saveSubdocsPreSave ( next ) {
9
+ schema . s . hooks . pre ( 'save' , false , async function saveSubdocsPreSave ( ) {
12
10
if ( this . $isSubdocument ) {
13
- next ( ) ;
14
11
return ;
15
12
}
16
13
17
14
const _this = this ;
18
15
const subdocs = this . $getAllSubdocs ( { useCache : true } ) ;
19
16
20
17
if ( ! subdocs . length ) {
21
- next ( ) ;
22
18
return ;
23
19
}
24
20
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
+ } ) ;
28
27
} ) ;
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
+ }
39
34
} , null , unshift ) ;
40
35
41
36
schema . s . hooks . post ( 'save' , async function saveSubdocsPostDeleteOne ( ) {
You can’t perform that action at this time.
0 commit comments