-
Notifications
You must be signed in to change notification settings - Fork 95
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
Promise.prototype.finally #18
Comments
Absent from concurrency strawman, due only to oversight. I don't have experience with it. Sounds plausible. Can you point at some examples that illustrate well how it is useful? |
Here is one quick example, where we use it to clean up event listeners: https://gist.github.com/domenic/6419679 |
Nice.
|
If we think we can get consensus on this, seems fine, otherwise, please lets not add new features that will delay the whole thing longer. |
Thanks for reining us in, @annevk :). My new position then is to default to not speccing it unless there's a sudden upswell of support due to your email this morning. We can always make it a high-priority addition later. I'll close this in a day or so if said upswell does not appear. |
I'd like to voice support for finally. I've had to implement it myself with the current version of DOM Promises and having it built-in would allow us to remove that piece of code which is basically there to restore a functionality that exists in synchronous operations. |
Closing for now. I would like this a lot but getting something done is more important. |
Another use-case for this is hiding a spinner after a network request succeeds or fails https://gist.github.com/jakearchibald/785f79b0dea5bfe0c448 |
@jakearchibald Thanks. I often use var HTTP = require("q-io/http");
var server = HTTP.Server(app);
return server.listen(0)
.then(function () {
// run test
})
.finally(server.stop) |
Minor correction: |
@rauschma no it is correct, as it retains the this from the containing closure. // es3+ version
'finally': function(callback) {
var constructor = this.constructor;
return this.then(function(value) {
return constructor.resolve(callback()).then(function(){
return value;
});
}, function(reason) {
return constructor.resolve(callback()).then(function(){
throw reason;
});
});
} |
oops i misread @rauschma you are correct! |
Fixed the OP |
@domenic i suppose class syntax will make that nice again :) |
@stefanpenner Object.assign(Promise.prototype, {
finally(callback) {
...
}
}); Versus: Promise.prototype.finally = function (callback) {
...
}; |
Quick flyby - I've packaged up @stefanpenner's implementation into something resembling a polyfill (and optimistically described it as one) for browser/node:- |
I was surprised to see |
@theefer follow the steps at the end of https://github.com/tc39/ecma262 to make it happen. |
@annevk is there a process to follow to make it NOT happen? :) I think |
Why? |
@getify easy, choose to not use it More seriously, I see myself as a strong advocate for |
From my perspective: people ask for it all the time in StackOverflow. It's also pretty useful for cleanup, logging etc. |
doesn't mean it should exist.
this is absolutely the scenario i believe requires it. As it is non-trivial and error prone to try an implement |
@getify argue against it on es-discuss. However, given that synchronous JavaScript already has try/catch/finally, it seems asynchronous JavaScript should offer the same. |
I didn't mean to hijack this already-closed thread to have such a debate. My troll to @annevk was a gentle nudge just to point out that
I know. I thought it would obviously be seen as a lighthearted troll. Hence the ":)".
Actually, with all due respect, this is one of the poorer arguments I've seen in favor of In With an asynchronous operation, there isn't nearly as clear of an answer when
There's lots more detail here. I won't vomit it all here for some sake of brevity. The point is, it's much more complicated than "well, |
(perhaps we should now move the debate elsewhere?) I don't have a particular problem with the concept of "cleanup" via But my problem with the current formulation of I believe an individual promise being well-defined as a singular immutable future-value container is the proper design. If you want to clean up after a single promise, you just do similar as the OP did above, in which case a But when you start chaining promises together, approximating async flow control, I believe you've stepped up into another layer of abstraction. The problem is that ES6 Promise is only giving us the individual promise abstraction, and when you start trying to reason about the entire chain as a single entity, there's lots of "limitations" that arise. I believe If we want to reason about BTW, I believe this is all quite similar to the reasoning that's driving ES7 proposals around |
I am not sure why this wasn't in the original DOM spec, nor in @erights's concurrency strawman, but it's damn useful and not trivial to implement yourself.
Notably, like JS
finally
clause:callback
).value
or rejected withreason
, just like the original one, unless...callback
throws, or returns a rejected promise), it propagates that failure onward, instead of the original success or failure.Use cases collected so far:
The text was updated successfully, but these errors were encountered: