Ela*_*nda 28 http promise observable angular
我有这个代码
return this.http.get(this.pushUrl)
.toPromise()
.then(response => response.json().data as PushResult[])
.catch(this.handleError);
Run Code Online (Sandbox Code Playgroud)
我想用observable而不是Promise
我怎样才能将错误返回给调用方法?
相当于 Promise.reject什么?
doSomeGet() {
console.info("sending get request");
this.http.get(this.pushUrl)
.forEach(function (response) { console.info(response.json()); })
.catch(this.handleError);
}
private handleError(error: any) {
console.error('An error occurred', error);
// return Promise.reject(error.message || error);
}
}
Run Code Online (Sandbox Code Playgroud)
调用方法是:
getHeroes() {
this.pushService
.doSomeGet();
// .then(pushResult => this.pushResult = pushResult)
// .catch(error => this.error = error);
}
Run Code Online (Sandbox Code Playgroud)
Gün*_*uer 30
private handleError(error: any) {
return Observable.throw('Some error information');
}
Run Code Online (Sandbox Code Playgroud)
mhx*_*mhx 12
随着RxJS 6 Observable.throw()改为throwError()
Observable.throw(new Error());
// becomes
throwError(new Error());
Run Code Online (Sandbox Code Playgroud)
来源:RxJS v5.x到v6更新指南 - Depracations
| 归档时间: |
|
| 查看次数: |
10174 次 |
| 最近记录: |