I have created a custom loaderService for my Angular application, that provides a loading spinner while a request (or a set of requests) is being handled by the back-end. Here is my code:
export class ComponentOne {
constructor(loaderService: LoaderService, orderService: OrderService) {}
...
...
method() {
this.loaderService.display(true);
this.orderService.firstRequest()
// the misspelled word causes a 500 error
.then((response) => this.orderService.methodWithWrongInput('misspelled_word'))
// but it still executes the following then
.then(() => this.method2())
.catch(() => null)
.then(() => this.loaderService.display(false));
}
...
}
I display the spinner before the request, but I want to make sure I close it no matter if the request succeeds or fails. That's why I put a catch and final then at the bottom. The code works fine, but when I was debugging it I realized that even though the second request (with the intentionally misspelled word) fails, method2() is still being called. Or does it actually fail?
My question is, how does the catch method work when chaining requests. I thought if a request fails, we go straight to the catch method, but I might be wrong.
Edit: Turns out I was wrong about method2() being called. See T.J. Crowder's answer for more.