RxJS捕获**并**重试一个Observable [英] RxJS catch **and** retry an Observable

查看:88
本文介绍了RxJS捕获**并**重试一个Observable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用例是将Observable映射到redux成功和失败的操作。我进行网络调用(带有承诺的函数),如果成功,我必须转发成功操作,如果失败而不是错误操作。 Observable本身将继续前进。对于我可以搜索的所有内容,RxJS没有用于捕获错误并重试原始文件的机制。我在我的代码中有以下解决方案,我不满意:

My use case is to map an Observable to redux actions of success and failure. I make a network call (with a function that gives promise), if it succeeds I have to forward a success action, if it fails than an error action. The Observable itself shall keep going. For all I could search, RxJS do not have a mechanism for this catching the error and retrying the original. I have following solution in my code which I am not happy with:

error$ = new Rx.Subject();

searchResultAction$ = search$
    .flatMap(getSearchResultsPromise)
    .map((resuls) => {
        return {
            type: 'SUCCESS_ACTION',
            payload: {
                results
            }
        }
    })
    .retryWhen((err$) => {
        return err$
            .pluck('query')
            .do(error$.onNext.bind(error$));
    });

searchErrorAction$
    .map((query) => {
        return {
            type: 'ERROR_ACTION',
            payload: {
                query,
                message: 'Error while retrieving data'
            }
        }
    });

action$ = Observable
    .merge(
        searchResultAction$,
        searchErrorAction$
    )
    .doOnError(err => console.error('Ignored error: ', err))
    .retry();

action$.subscribe(dispatch);

即我创建一个主题,并将错误推送到该主题并从中创建一个Observable错误操作。

i.e I create a subject, and push errors into that subject and create an Observable of error actions from that.

在RxJS中有更好的替代方法吗?基本上我想发出一个发生了什么错误的通知,然后继续Observable已经做的任何事情。

Is there a better alternative of doing this in RxJS that I am missing? Basically I want to emit a notification of what error has occurred, and then continue with whatever the Observable is already doing.

推荐答案

这个将重试失败的查询:

This would retry failed queries:

var action$ = search$
    .flatMap(value => {
        // create an observable that will execute
        // the query each time it is subscribed
        const query = Rx.Observable.defer(() => getSearchResultsPromise(value));

        // add a retry operation to this query
        return query.retryWhen(errors$ => errors$.do(err => {
            console.log("ignoring error: ", err);
        }));
    })
    .map(payload => ({ type: "SUCCESS_ACTION", payload }));

action$.subscribe(dispatcher);

如果您不想重试,只想通知或忽略错误:

If you don't want to retry, but just want to notify or ignore errors:

var action$ = search$
    .flatMap(value => {
        // create an observable that will execute
        // the query each time it is subscribed
        const query = Rx.Observable.defer(() => getSearchResultsPromise(value));

        // add a catch clause to "ignore" the error
        return query.catch(err => {
            console.log("ignoring error: ", err);
            return Observable.empty(); // no result for this query
        }));
    })
    .map(payload => ({ type: "SUCCESS_ACTION", payload }));

action$.subscribe(dispatcher);

这篇关于RxJS捕获**并**重试一个Observable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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