File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -163,3 +163,36 @@ const { promiseOrCallback } = require('mongoose');
163163
164164promiseOrCallback; // undefined in Mongoose 9
165165```
166+
167+ ## In isAsync middleware ` next() ` errors take priority over ` done() ` errors
168+
169+ Due to Mongoose middleware now relying on promises and async/await, ` next() ` errors take priority over ` done() ` errors.
170+ If you use ` isAsync ` middleware, any errors in ` next() ` will be thrown first, and ` done() ` errors will only be thrown if there are no ` next() ` errors.
171+
172+ ``` javascript
173+ const schema = new Schema ({});
174+
175+ schema .pre (' save' , true , function (next , done ) {
176+ execed .first = true ;
177+ setTimeout (
178+ function () {
179+ done (new Error (' first done() error' ));
180+ },
181+ 5 );
182+
183+ next ();
184+ });
185+
186+ schema .pre (' save' , true , function (next , done ) {
187+ execed .second = true ;
188+ setTimeout (
189+ function () {
190+ next (new Error (' second next() error' ));
191+ done (new Error (' second done() error' ));
192+ },
193+ 25 );
194+ });
195+
196+ // In Mongoose 8, with the above middleware, `save()` would error with 'first done() error'
197+ // In Mongoose 9, with the above middleware, `save()` will error with 'second next() error'
198+ ```
You can’t perform that action at this time.
0 commit comments