Using Angular's http client with complete in the subscribe (or the finalize pipe), the code is never executed.
Simple example:
this.http.get(myUrl).subscribe({
next: results => handleSuccess(results),
error: err => handleError(err),
complete: () => handleAllRequests()
The handleAllRequests() function is never called. Adding the finalize pipe or an additional `.add() after the subscription per other answers does not work.
Similar questions: Angular 6 / Rxjs - how to basics: observables success, error, finally Angular HttpClient observable not completing
After some digging, I found the answer to be simple but not obvious (to me at least).
It came down to understanding when an observable "completes". As long as the subscription existings (is not unsubscribed or observer.complete() is not called), the complete code and finalize pipe are not called.
Therefore, you must "complete" the subscription. With Angular http client, I chose to use take(1) which will complete the subscription after one execution. I use this pattern frequently for http requests as it is normally the goal to make the request once in the first place.
While the above is true, I found that my real issue was that a teammate added an interceptor with a switchMap that was not completing, which effectively caused every HTTP request to remain incomplete unless forced like the suggestion above (using a take(1), etc.)
I hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With