I always chain a `then` after `catch` in these situations.
fetch(...)
.then( response => data.response = response )
.catch( error => data.error = error )
.then( () => closeThatLoader() )I always chain a `then` after `catch` in these situations.
fetch(...)
.then( response => data.response = response )
.catch( error => data.error = error )
.then( () => closeThatLoader() ) fetch(...)
.then(response => {
closeThatLoader();
return success(response);
})
.catch(error => {
closeThatLoader();
throw error;
});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.
(It's more apparent too in the async/await space where catch is even literally try {} catch {}.)
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.
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.
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.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().
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.