简体   繁体   中英

How to retry fetching after refresh token using redux-observable?

I'm new in RxJS and I faced with need to retry fetching items to api if it responded with unauthorized error. I need to call refresh token endpoint and then retry fetching. How can I do it using epics from redux-observable? I guess it should looks like this code, but I'm not sure what to do in catchError block.

export const getItemsEpic = (action$) =>
action$.pipe(
    ofType('FETCH_ITEMS'),
    switchMap(() => getItemsRequest()),
    map((response) => addItemsAction(response.data)),
    catchError(err => {
        ???
    }),
);

You can try to add a pipe like that

export const getItemsEpic = (action$) =>
action$.pipe(
  ofType('FETCH_ITEMS'),
  switchMap(() => getItemsRequest()),
  map((response) => addItemsAction(response.data)),
  catchError(error => {
    // some logic how to refresh to token.
    return ajax('refreshtoken').pipe(switchMapTo(throwError(error)));
  ),
  retry(1), // allows only 1 failure.
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM