cancel all pending requests angular4

587 Views Asked by At

How can I cancel all pending promises in angular2. I know that promises are not cancellable, but is there some work around for this?How can I cancel all pending requests on logout.

api-service

get(url: string) {
        let that = this;
        let getPromise = new Promise((resolve, reject) => {
            this
                .httpService
                .get(url)
                // ...and calling .json() on the response to return data
                .map((res: Response) => res.json())
                .subscribe(response => {
                    resolve(response);
                }, (error) => {
                    console.log(error);
                    reject(that.handleError(error));
                }, () => {
                    console.log('API Subscription Complete!');
                });
        });
        return getPromise;
    }

component-service

fetchData(url) {
        return new Promise(resolve => {
            this
                .apiService
                .get(url)
                .then(response => {
                    resolve(response);
                }).catch(error=>{
                    console.log(error);
                });
        });
    }
1

There are 1 best solutions below

3
Pardeep Jain On

You can use unsubscribe(); of observable

subscribeRequest.unsubscribe();

for more information please refer This article