How to cancel/abort all pending HTTP requests angular 4+.
There is an unsubscribe
method to cancel HTTP Requests but how to cancel all pending requests all at once.
Especially while route change.
There is one thing I did
ngOnDestroy() {
this.subscription.unsubscribe();
}
but how to achieve this globally
Any Ideas?
Checkout the
takeUntil()
operator from RxJS to globally drop your subscriptions :- RxJS 6+ (using the
pipe
syntax)- RxJS < 6
You can basically emit an event on your unsubscribe
Subject
usingnext()
everytime you want to complete a bunch of streams. It is also good practice to unsubscribe to active Observables as the component is destroyed, to avoid memory leaks.Worth reading :
Avoiding take until leaks
A great answer from seangwright
Try This :