I am new to rxjs and working with redux-observable and have a question about where to use the catchError function from rxjs.
I have the following code working how I want it to:
function countEpic(action$: Observable<AnyAction>): Observable<AnyAction> {
return action$
.pipe(
map(logEpic)
,ofType(RESET_REQUEST)
,flatMap(action =>
resetCountService(action)
.pipe(
map(logEpic)
,map(newCount => ({ type: RESET_SUCCESS, payload: newCount }))
,catchError((error: any, caught: Observable<AnyAction>) => of({ type: RESET_FAILURE }))
)
)
);
}
function resetCountService(action: AnyAction): Observable<number> {;
return of(7)
.pipe(
map(newCount => { throw "broke" })
);
}
NOTE: the resetCountService is just a mock service in place of an API service that would return an Observable in the future
before I had
function countEpic(action$: Observable<AnyAction>): Observable<AnyAction> {
return action$
.pipe(
map(logEpic)
,ofType(RESET_REQUEST)
,flatMap(resetCountService)
,map(newCount => ({ type: RESET_SUCCESS, payload: newCount }))
,catchError((error: any, caught: Observable<AnyAction>) => of({ type: RESET_FAILURE })))
)
);
}
In my code (the before I had code) the catchError seemed to "stop" the Observable pipe from continuing and I'm not sure why. My question is why do I need to catchError on the "inner" Observable rather than catching it on the epic Observable? All answers and links are appreciated!