From 7629c94037c79fa07640fec77aacf9c25b6e8e96 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 9 Apr 2024 11:55:30 -0400 Subject: [PATCH 1/3] docs(middleware): add note about using async functions for post hooks --- docs/middleware.md | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/middleware.md b/docs/middleware.md index 41440c9a6bc..e6dc8d30474 100644 --- a/docs/middleware.md +++ b/docs/middleware.md @@ -250,9 +250,7 @@ schema.post('deleteOne', function(doc) {

Asynchronous Post Hooks

-If your post hook function takes at least 2 parameters, mongoose will -assume the second parameter is a `next()` function that you will call to -trigger the next middleware in the sequence. +If your post hook function takes at least 2 parameters, mongoose will assume the second parameter is a `next()` function that you will call to trigger the next middleware in the sequence. ```javascript // Takes 2 parameters: this is an asynchronous post hook @@ -271,6 +269,25 @@ schema.post('save', function(doc, next) { }); ``` +You can also pass an async function to `post()`. +If you pass an async function that takes at least 2 parameters, you are still responsible for calling `next()`. +However, you can also pass in an async function that takes less than 2 parameters, and Mongoose will wait for the promise to resolve. + +```javascript +schema.post('save', async function(doc) { + await new Promise(resolve => setTimeout(resolve, 1000)); + console.log('post1'); + // If less than 2 parameters, no need to call `next()` +}); + +schema.post('save', async function(doc, next) { + await new Promise(resolve => setTimeout(resolve, 1000)); + console.log('post1'); + // If there's a `next` parameter, you need to call `next()`. + next(); +}); +``` +

Define Middleware Before Compiling Models

Calling `pre()` or `post()` after [compiling a model](models.html#compiling) From f76c7e308c4ffe385af8211c122670c2c4a45efe Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 9 Apr 2024 11:55:50 -0400 Subject: [PATCH 2/3] test(types): add test case for using `post(save)` with no `next()` arg --- test/types/middleware.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/types/middleware.test.ts b/test/types/middleware.test.ts index e127c3b683b..2cb24ba929f 100644 --- a/test/types/middleware.test.ts +++ b/test/types/middleware.test.ts @@ -63,6 +63,10 @@ schema.post('save', function() { console.log(this.name); }); +schema.post('save', async function() { + console.log(this.name); +}); + schema.post('save', function(err: Error, res: ITest, next: Function) { console.log(this.name, err.stack); }); From bac8fb01e832efcf5663109c95cf52f3a22b11a9 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 9 Apr 2024 11:57:34 -0400 Subject: [PATCH 3/3] docs(middleware): remove archaic reference to node 7.6 in async/await example --- docs/middleware.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/middleware.md b/docs/middleware.md index e6dc8d30474..03a40983a7a 100644 --- a/docs/middleware.md +++ b/docs/middleware.md @@ -151,7 +151,7 @@ schema.pre('save', function() { then(() => doMoreStuff()); }); -// Or, in Node.js >= 7.6.0: +// Or, using async functions schema.pre('save', async function() { await doStuff(); await doMoreStuff();