Skip to content

Commit e32a1bd

Browse files
committed
add note re: mongoosejs/kareem#39
1 parent 0b79373 commit e32a1bd

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

docs/migrating_to_9.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,36 @@ const { promiseOrCallback } = require('mongoose');
163163

164164
promiseOrCallback; // 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+
```

0 commit comments

Comments
 (0)