-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
Return a Promise from vm.$once() with callback omitted #8444
Comments
Isn't this doable in userland (non tested): const { $once } = Vue.prototype
Vue.prototype.$once = function once (event, cb) {
if (cb == null) return new Promise(resolve => $once.call(this, resolve))
$once.call(this, cb)
} |
This in fact works, but I wouldn't feel comfortable at all to modify built-in functionality like that to be honest. |
Would you be more comfortable if I hide it behind a plugin 😄 ? |
Only if you didn't tell me and I didn't know. 🙈 Which is pretty contrary to using it though. 😁 |
FWIW, the change to |
It was only half a joke though.
|
I'd like to humbly disagree. While The point about not knowing if the Promise will ever resolve makes me feel uncomfortable as well, but I don't think that's a very rational objection. It's not really different from a However, I think there's no obligation for us to agree on that topic. Thanks for discussing this issue with me. 🙂 P.S.: FWIW, the Node.js team currently has basically the same discussion going on. |
What problem does this feature solve?
Deferring a process to wait for a signal currently is a little bit tedious. Signals are usually carried as events and reacted to via
vm.$on()
resp.vm.$once()
.Now that we have
async/await
, we could improve developer ergonomy greatly by having a Promise returned fromvm.$once()
– similar tovm.$nextTick()
.What does the proposed API look like?
So what we'd want to do is deferring the execution of our code until the
signal
event is emitted by our Vue instance.A quick recap how this could currently be done:
How this would look with the proposed change:
Note that a Promise would only be returned if the callback parameter was omitted, so the following still works:
While this is technically a breaking change, it's a rather soft one since previously using the
$once()
method without a callback had no use case.To me, this change sounds like pretty low-hanging fruit. I'd love to hear your opinion regarding this idea.
The text was updated successfully, but these errors were encountered: