RxJS catch ** and ** repeat observation

My use case is to compare what is observed with the reduction actions of success and failure. I am making a network call (with a function that makes a promise), if it succeeds, I need to direct the action to succeed if it is not completed, than the action with the error. Compliance itself must continue. For all that I could perform the search, RxJS has no mechanism for this error correction and restarting the original. In my code there is the following solution which does not suit me:

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); 

ie I create a topic and insert errors into this topic and create an Observable from errors.

Is there a better alternative for this in RxJS that I am missing? Basically, I want to issue a notification of an error that has occurred, and then continue with what Observable is already running.

+5
source share
1 answer

This will cause the request to fail again:

 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 do not want to try again, you just want to report or ignore the 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); 
+9
source

Source: https://habr.com/ru/post/987741/


All Articles