Skip to content

BREAKING CHANGE: make execPre async and drop callback support #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

vkarpov15
Copy link
Member

Work in progress to make execPre() and execPost() use async functions where possible to better support Automattic/mongoose#15317. Tested in kareem repo as well as running Mongoose with these changes.

Couple of breaking changes in this PR:

  1. No longer support passing arguments to the next middleware. Previously, you could do next(null, 'new arg') and the args to the next middleware would get overwritten by 'new arg'.
  2. No more callback support for execPre()
  3. Errors take precedence over next() calls. If your middleware function does something like next(); throw new Error('Fail');, this new logic will treat the middleware function as errored with error 'Fail'. Kareem@2 ignores the 'Fail' error.

@hasezoey your feedback would be much appreciated.

Copy link
Member

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me, though as i have not worked with this much, i was quite confused about isAsync vs returning a Promise, but now i understand isAsync means to run it in parallel instead of waiting before calling the next hook. Maybe add some documentation to the isAsync parameters?

Another question that came up, should next be deprecated in favor of using Promises?

Also the documentation (just the README?) needs to be updated to reflect this (btw the README contains some acquit:ignore:end)

PS: if this PR is a WIP, maybe mark it as such?

vkarpov15 added a commit to Automattic/mongoose that referenced this pull request Apr 28, 2025
@@ -28,8 +28,7 @@ Kareem.overwriteResult = function overwriteResult() {
* Execute all "pre" hooks for "name"
* @param {String} name The hook name to execute
* @param {*} context Overwrite the "this" for the hook
* @param {Array|Function} args Optional arguments or directly the callback
* @param {Function} [callback] The callback to call when executing all hooks are finished
* @param {Array|Function} args arguments passed to the pre hooks
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm, this should still include Function even though the callback is removed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants