捕获Observable会停止来自Observable.interval的HTTP调用 [英] Catch in Observable stops HTTP Calls from Observable.interval

查看:329
本文介绍了捕获Observable会停止来自Observable.interval的HTTP调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Observable.interval(10000)
     .switchMap(() => this.http.get(url))
     .catch (err => Observable.empty())
     .subscribe(data => render(data))

每10秒钟我们进行一次HTTP调用.如果发生错误,observable将完成,不再进行任何调用.如何预防呢?

Each 10 seconds we make an HTTP call. If an error happens, observable becomes completed, it doesn't make any calls anymore. How to prevent that?

推荐答案

这是正确的行为,当发送completeerror通知时,观察者将取消订阅并处理该链.

That's correct behavior, when a complete or error notification is sent observers unsubscribe and the chain is disposed.

您可以使用retry()运算符重新订阅,但是很难从这个简短的说明中看出您的目标.

You can use the retry() operator to resubscribe but it's hard to tell what is your goal from this brief description.

Observable.interval(10000)
    .switchMap(() => this.http.get(url))
    .retry()
    .subscribe(data => render(data))

这篇关于捕获Observable会停止来自Observable.interval的HTTP调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆