Jeeten Parmar
Jeeten Parmar

Reputation: 5787

Cancel /Kill http api call angular 2

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?

Upvotes: 5

Views: 3561

Answers (2)

user966614
user966614

Reputation: 191

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

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

Upvotes: 1

Stefan Svrkota
Stefan Svrkota

Reputation: 50643

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();
}

Upvotes: 4

Related Questions