【发布时间】:2019-04-13 00:02:03
【问题描述】:
这与我遇到的错误无关,而是语法问题。
工作流程很简单:
- 发出返回布尔值的 HTTP 请求
- 如果布尔值为真,则继续
- 如果布尔值为假,则记录警告并停止流。
为了管理它,我当前的代码是这样的:
样板文件
private _getBoolean() { return this.http.get(...); }
private _getData() { return this.http.get(...); }
当前代码
public getData() {
return this._getBoolean().pipe(
filter(bool => {
if(!bool) {
console.warn('Wrong server answer, stream stopped');
return false;
}
return true;
}),
switchMap(bool => this._getData())
);
}
我不知道为什么,但它对我来说并不自然和优化。
我认为会有一些东西可以简化语法,就像这样
public getData() {
return this._getBoolean().pipe(
throwError(bool => bool ? new Error('Wrong server answer, stream stopped') : null),
catchError(err => console.warn(err)),
switchMap(bool => this._getData())
);
}
有没有类似的东西,或者我有正确的语法?
【问题讨论】:
标签: angular typescript rxjs rxjs-pipeable-operators