如何仅重试 RxJs 中源 observable 发出的某些错误 [英] How to retry only on certain error emitted by the source observable in RxJs

查看:47
本文介绍了如何仅重试 RxJs 中源 observable 发出的某些错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

srcObservable.retry() 将捕获由 srcObservable 发出的错误并重新订阅 srcObservable,而不管错误的类型.但是,在某些情况下,只希望重试 srcObservable 发出的某些类型的错误.有没有办法在 RxJs 中巧妙地做到这一点?

A srcObservable.retry() will catch the error emitted by the srcObservable and resubscribe to the srcObservable regardless of the type of the error. However, on certain scenario, it is wanted to only retry on certain type of error emitted by the srcObservable. Is there a way to do so in RxJs neatly?

推荐答案

尝试使用 retryWhen:

src.retryWhen(function (errors) {
    // retry for some errors, end the stream with an error for others
    return errors.do(function (e) {
        if (!canRetry(e)) {
            throw e;
        }
    });
});

这篇关于如何仅重试 RxJs 中源 observable 发出的某些错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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