我有这个结构:
可注射服务apiHelper.ts
@Injectable()
export class ApiHelper {
private headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded'});
tokenRequest(data: any, path: string): Promise<any> {
return this.localStorage.getAccessToken().then(token => {
if (this.connectivityService.isOnline()) {
if (token) {
data["user_token"] = token
return this.http.post(path, this.getParams(data), {headers: this.headers})
.toPromise()
.then(response => {
return response.json();
})
.catch(this.handleError);
} else {
throw {Error: "errorToken"}
}
}
else {
this.messagePresenter.presentToast("no connection", 2000, "bottom")
throw {Error: "noconn"}
}
})
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', JSON.stringify(error)); // for demo purposes only
return Promise.reject(error);
}
}
这是来自某个页面的一些请求
this.api.obtainData({}).then(response =>{
console.log(response)
})
.catch(error =>{
console.log("error")
});
我的问题是当互联网连接不可用时,throws
不会传播到请求捕获。
我如何强制拒绝来自非catch块代码的承诺?
感谢