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);
});
});
}
You can use
unsubscribe();of observablefor more information please refer This article