5

Sometimes http api call takes long time to load data. In this case, if we move on another component, it still keeps executing (we can see it in browser console). So, is there any way by which we can cancel or kill http api call when we move on another component?

2 Answers 2

4

You can "kill" it by using unsubscribe() method in OnDestroy lifecycle event, under assumption you are using subscriptions, for example:

mySubscription: any;

ngOnInit() {
    this.mySubscription = this.myHttpCall().subscribe...
}

ngOnDestroy() {
    this.mySubscription.unsubscribe();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you... :) By using this, I can unsubscribe() one api but is there any way by which i can unsubscribe() multiple apis by using just one variable.
You can use the takeWhile() function from rxjs to unsubscribe all the subscriptions: Example: brianflove.com/2016/12/11/anguar-2-unsubscribe-observables
1

You can use the takeWhile() function from rxjs to unsubscribe all the subscriptions: Example:

http://brianflove.com/2016/12/11/anguar-2-unsubscribe-observables/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.