Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 357
Jeeten Parmar Avatar asked Jan 18 '17 12:01

Jeeten Parmar


1 Answers

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();
}
like image 85
Stefan Svrkota Avatar answered Sep 21 '22 18:09

Stefan Svrkota