【发布时间】:2016-12-23 22:46:46
【问题描述】:
我有这个代码
return this.http.get(this.pushUrl)
.toPromise()
.then(response => response.json().data as PushResult[])
.catch(this.handleError);
我想使用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);
}
}
调用方法是:
getHeroes() {
this.pushService
.doSomeGet();
// .then(pushResult => this.pushResult = pushResult)
// .catch(error => this.error = error);
}
【问题讨论】:
-
上面带有
Promise.reject未注释的代码工作得很好。我不确定为什么。你有什么想法吗?
标签: http angular promise observable