取消所有未决请求angular4

时间:2018-02-02 05:31:24

标签: angular2-services angular2-http

如何取消angular2中所有待处理的承诺。我知道承诺不可取消,但有没有解决这个问题?如何在注销时取消所有待处理的请求。

API-服务

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

组件的服务

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

1 个答案:

答案 0 :(得分:0)

您可以使用unsubscribe();的可观察

subscribeRequest.unsubscribe();

有关详细信息,请参阅This article