News for hackers. Stuff that matters.
Re:Promise.prototype.finally by drinchev on Monday October 23, 2017 @11:53AM (#15534087)
> In either case we need to call hideLoadingSpinner(). Until now, we had no choice but to duplicate this call in both the then() and the catch() block.

I always chain a `then` after `catch` in these situations.

    fetch(...)
        .then( response => data.response = response )
        .catch( error => data.error = error )
        .then( () => closeThatLoader() )
Re:Promise.prototype.finally by runarberg on Monday October 23, 2017 @11:59AM (#15534138)
Another upside of the proposal is that if you need to reuse the fetch promise somewhere else it will retain the reject status if the promise fails. The proposal is more akin to:

    fetch(...)
      .then(response => {
        closeThatLoader();
        return success(response);
      })
      .catch(error => {
        closeThatLoader();
        throw error;
      });
Re:Promise.prototype.finally by semperdark on Monday October 23, 2017 @02:28PM (#15535627)
Am I the only one that really dislikes the "catch" behavior?

The fact that catching a promise marks it as resolved and triggers subsequent "then"s rather than "catch"s. It means that every callback needs to be aware of every other callback and their own place in the order, so that only the last catch statement doesn't rethrow the exception.

Something like $.Deferred() allows you to set 10 "fail" callbacks independently, which helps a lot when the original promise is generated by a deeply nested method and different stages of the chain add their own handling.

Re:Promise.prototype.finally by WorldMaker on Monday October 23, 2017 @02:43PM (#15535775)
The catch behavior mirrors try {} catch {} blocks. If you don't need a catch, don't catch, and let it bubble up. Try to keep the things you catch to a bare minimum. The rethrow decision is largely the same for Promises as try {} catch {} blocks, too.

(It's more apparent too in the async/await space where catch is even literally try {} catch {}.)

Re:Promise.prototype.finally by semperdark on Monday October 23, 2017 @03:03PM (#15535995)
Thanks for the reply. I guess my contention as someone just starting to use vanilla promises is that it feels like try/catch isn't the appropriate metaphor to use here.

In my specific case I'm passing a lot of promises to user-made scripts. then/catch seems to make things more brittle. As I said though, I'm new to them.

Re:Promise.prototype.finally by WorldMaker on Monday October 23, 2017 @04:56PM (#15536872)
That's totally fair. There are learning curves involved and sometimes tradeoffs aren't entirely obvious why they were made.

If you get a chance, particularly play with async/await code somewhere. There's a lot of misconception that async/await replaces Promises or is very different from Promises, but the reality is much closer that Promises were a necessary building block for async/await, and some of the trade-offs in Promise design are there to support async/await, based on work that had already been done in other languages.

Maybe if you try to write some of your work as async/await it might give you insights into your architecture and how to make it feel less brittle.

Re:Promise.prototype.finally by Tarean on Monday October 23, 2017 @04:29PM (#15536697)
That is assuming that you chain promises like

    a
    .then(b).catch(bhandle)
    .then(c).catch(chandle)
instead of

    a
    .then(aout => b(aout).catch(bhandle))
    .then(bout => c(bout).catch(chandle))
which admittedly is pretty ugly in javascript.
Re:Promise.prototype.finally by riquito on Monday October 23, 2017 @02:11PM (#15535463)
You may want to do something _and then_ throw the error, to let it reach the caller. In your example it would mean to run "closeThatLoader" and later throw the error. It's really cumbersome without `finally`
Re:Promise.prototype.finally by mathias on Monday October 23, 2017 @04:55PM (#15536860)
That’s not equivalent though — in the article’s example, imagine `element` is `undefined`, for example. `then()` wouldn’t be called in that case.
Re:Promise.prototype.finally by Kiro on Monday October 23, 2017 @11:57AM (#15534121)
Yes, trying to understand what the point of .finally is when a subsequent .then after .catch seems to achieve the same thing. What am I missing?
Re:Promise.prototype.finally by untog on Monday October 23, 2017 @12:42PM (#15534525)
A final .catch().then() swallows the error, .always() does not.
Re:Promise.prototype.finally by joshribakoff on Monday October 23, 2017 @01:28PM (#15534973)
Catch "swallows" errors by design. Unless you re throw. chaining another then has no effect on the behavior of a previous catch.
Re:Promise.prototype.finally by untog on Monday October 23, 2017 @01:34PM (#15535040)
Right, but in the context of the OP's question you'd need the second .then() to mirror what .always() is doing.

The point is, there is no way to do .catch().then() and throw the error unless you do some hideous type checking hack cludge. Hence the need for .always().

Re:Promise.prototype.finally by JSDave on Monday October 23, 2017 @02:34PM (#15535695)
Yea, the `catch` would probably have to set an 'errors' object on the response. The following `then` would then decide whether or not to throw an error based on that. Not pretty.

Also, I think `finally` makes your code easier to read. I always put the same kind of cleanup logic in there, everyone on the team knows what to expect in that block.

Re:Promise.prototype.finally by bbatha on Monday October 23, 2017 @12:51PM (#15534601)
If you rethrow in the catch the next then won't get executed.
Re:Promise.prototype.finally by Kiro on Tuesday October 24, 2017 @02:32AM (#15539660)
Will finally be executed between the throw and whatever catches it or after?
Re:Promise.prototype.finally by eknkc on Monday October 23, 2017 @12:16PM (#15534299)
If you are returning that promise, you'll need to handle both then and catch so the caller can chain their own catch.