-
Notifications
You must be signed in to change notification settings - Fork 546
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
Allow guards to return a promise instead of calling the next
callback
#177
Comments
Love this idea, particularly since I have tripped up using Would a void return (or any falsey return aside from maybe empty string) be equivalent to |
next
callback
That's a very very good point. I personally think falsey return should mean abort. But i can also see it being annoying to do |
I don't think falsy returns should abort because by default, no return is |
I was wrong with this being a breaking change, we can have both syntaxes. Here is the implementation: vuejs/router#343 |
@posva That's great! Being able to simply return a value (sync or async) also looks and feels simpler than callbacks, it's a nice improvement! ❤️ |
Thanks! |
yay! how exciting! |
@posva haha thanks! sorry for not writing up an rfc for this. I'm lazy. |
Currently, vue router guards rely on the
next
callback function to identify the decision of the guard. E.g.next(error)
to throw an error;next(false)
to abort;next()
to continue;next(path)
to redirect to other paths.I argue that this
next
callback style is quite an old fashioned way of doing things in JS and no longer idiomatic. I believe the main reason for thisnext
callback was for asynchronous operations, just like most othernext
callbacks in other lib and apis.Before filing an RFC, I would like to collect some opinion on replacing
next
withreturn
(sync or async) andthrow
.Screenshot from https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards
We currently have 4 variations of
next
. and I believe we can replace the first 3 withreturn
and the last one withthrow
.For example (copied from https://css-tricks.com/protecting-vue-routes-with-navigation-guards/):
Can become
The benefits I can think of right now:
next()
should be called only once. As shown in the image above.I can't wait to hear your thoughts.
The text was updated successfully, but these errors were encountered: