-
Notifications
You must be signed in to change notification settings - Fork 27.4k
promise finally function could send the success/fail response as parameter #9246
Comments
I think you are getting confused here, if you have a promise, then calling foo = $http(...);
foo.then(...);
foo.finally(...); With this information, this issue is invalid |
@lgalfaso : I think you misunderstood the problem. NOTE: In the code above I'm just linking the calls directly, without using a "foo" variable. The problem is that the .finally() callback doesn't receive any parameter. My suggestion was for finally to receive whatever parameters would receive the 2 callbacks: error or success. Or maybe one more which says if the flow was the success or error one (as angular internal methods). PLEASE REOPEN. |
@dragosrususv if you do ajax.call($scope.shared.ajax.getXXX)
.then(function (response) { /* stuff */ return response; })
.finally(msgService.notify); then |
@lgalfaso: The thing is that they need to do it from both success and error callbacks. If it was passed automatically, then their code would be DRYier by 1 line :) |
@gkalpak the fact that Just add another method to function sameSameAsThenButDifferent(callback, errback, progress back) {
this.then(callback, errback, progress back);
return this;
} and use it |
@lgalfaso: I wasn't referring to the What the OP is trying to do is perform some operation when a request completes (either successfully or with error) and this operation depends on the response itself. So, instead of having to perform the operation in two places or having to return the response from both the onSuccess and onError callbacks and create a new promise, they ask that the response be passed to the This feature does not need to be specific to Anyway, I am just trying to make sure the request is clear, so the decision (if it's going to be added or not) is made on the correct assumptions. |
@gkalpak if you have a promise
The fact that when you call |
@lgalfaso: I don't think anyone wants to reference another promise. E.g.:
|
@gkalpak that is not what function myCallback(val) {
console.log('Finally, I was called with argument:', val);
}
var deferred = $q.defer();
deferred.promise.then(undefined, angular.identity).then(myCallback);
deferred.resolve('Hello, world !'); /* or */ deferred.reject('Hello, world !'); |
@lgalfaso: That said, I suppose this is a "won't fix" (and having to add the same line of code twice isn't so much of a nuisance anyway). Let's leave it at that :) |
@lgalfaso : please mind that on error case scenario you don't have a 2nd callback in last .then (".then(myCallback);") - this means that if the promise is rejected, your code will just not do anything on this flow. The only solution is via finally which should receive the last returned value from either success or failure callbacks. Again PLEASE reopen the item and leave others to share their opinion on it. |
I don't really have a problem with making it work if someone wants to send a PR. the finally implementation is sort of confusing and hard to read, and might not be too high on the list of priorities right now. If you submit a fix for this I'm happy to review it. |
@caitp : seems @shahata already did a PR for this - for you to reopen this issue and review. I added a small comment to @shahata's PR, but it's not a blocker I guess - would just make more sense if we think to the base programming languages where parameters are always the same type. |
This is a very bad idea. |
@domenic : can you stop spamming opened issues in multiple projects and provide solutions? I'm not keen on having the "finally" keyword used, I'm keen on having this option of executing a shared piece of code in resolved or rejected mode and stop copy-pasting between (even a 1-liner). Do you have any suggestions here? |
Yes. This will work: doThing().catch(() => {}).then(doOtherThing); it is equivalent to try {
doThing();
} catch (e) { }
doOtherThing(); |
Again, please stop spamming this issue as you posted this kriskowal/q#589 as well. I have the feeling that you don't understand the problem (see comments above), but I might be wrong of course. |
You may not be aware, but I am a maintainer of Q, and so it is my responsibility to respond there. |
:) @domenic : I am aware ("Collaborator"). But from your comments you seem to have fall in love with your code and you find it hard to think outside/without it. I respect that, but the aim here is to reduce the code footprint. As you mentioned in a parallel threat, the ".catch().then()" might be a solution, but again, I don't need a "catch." - I'm forced to add it to achieve this. And the global point is again to reduce the code footprint. |
lets try keep it respectful and try not to refer to comments from different people as "spam". I'm not totally sure how this feature would affect various apps or their codebases, so the input is definitely valuable |
@caitp : the only reason I labeled @domenic 's comments as spam was because their were repetitive (3: 2 github issue pages, 1 github PR) - and in all he just posted the same content. I just had to reply everywhere, even if there was a long discussion made on reported Q issue. This usually falls into the spam category. As for @domenic 's solution, as previously stated, seems like an workaround. Other people have added their constructive feedback here: #9287 (comment) - seems like a good place to start (another callback: ".always()"). |
< ping > @caitp |
Based on kriskowal/q#589 (comment) I think that we should not add an |
@lgalfaso : please provide a non throw-try-catch-workaround solution though - I'm open. As per other #9287 (comment) this can and should be done - one way or another. The only reason Domenic is pushing back is because he needs to wrap up the final spec and he cannot add something new (I don't understand that but he must have good reasons for it) on the last round. But this can be again base for a future spec. |
@dragosrususv I read the entire thread at kriskowal/q#589 and the only reference to a spec was on the change to make Now, back to In my specific case (and for sure I am not talking on behalf of Q nor bluebird) the Angular core is not the place to provide every solution to every problem. Third party libraries can decorate |
Type: feature
Reproduces on: any OS, any browser
Code:
https://github.com/angular/angular.js/blob/master/src/ng/q.js#L285
https://github.com/angular/angular.js/blob/master/src/ng/q.js#L460
Solution:
Just send the value as parameter in the callback. It could even have 2 params: success and value (as angularjs callbacks have).
Real use-case:
In our application all messages that are being queued and displayed to the user in a messages-directive are being sent by the server within each structured response (may it be success or error). Our problem is that we have to write something like:
Instead of just
CONTEXT:
I am aware that Kris's initial implementation of $q (https://github.com/kriskowal/q) does not have this, but I still believe it's a very good idea to implement. I can contact Kris directly and discuss with him the option to include this in base implementation as well.
The text was updated successfully, but these errors were encountered: