每个成功/错误调用上的RXJS回调
我有这样的方案,
this.uiDataObservable.pipe(
tap(() => this.showLoader.next(true)),
map((uiData) => this.createHttpRequest(uiData),
switchMap((httpRequest) => this.apiService.get(httpRequest),
map((response) => this.createUISucessData(response),
)
).subscribe(
(success) => /* show data on UI */,
(error) => /* show error on UI */,
);
现在在此示例中,有什么正确的位置this.showloader.next(false)
。
- 我不能使用
最终确定
,因为我的流永无止境。 - 我不能使用
subscribe
block的第三个回调完成,因为它从未 得到错误的电话。
正确的RXJS
处理这种情况的方法是什么?
I have a scenario like this,
this.uiDataObservable.pipe(
tap(() => this.showLoader.next(true)),
map((uiData) => this.createHttpRequest(uiData),
switchMap((httpRequest) => this.apiService.get(httpRequest),
map((response) => this.createUISucessData(response),
)
).subscribe(
(success) => /* show data on UI */,
(error) => /* show error on UI */,
);
Now in this example what is the right place to call this.showLoader.next(false)
.
- I cannot use
finalize
because my stream never ends. - I cannot use the third callback
complete
ofsubscribe
block as it never
gets calls on error.
What is the correct rxjs
way of handling this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定为什么使用RXJS回调应该比其他任何内容都不应视为RXJ。无论如何,为了不重复
this.showloader.next(false)
,您可以创建一个为您处理调用的自定义操作员。可能如下:
I'm not sure why using RxJS callbacks should be considered less RxJS than anything else. Regardless, in an effort to not repeat
this.showLoader.next(false)
, you could create a custom operator that handles the calls for you.That might look as follows:
找到更好的替代方案,代码是这样的:
Found better alternative, code goes like this: