简体   繁体   English

如何在使用 redux-observable 刷新令牌后重试获取?

[英]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.我是 RxJS 的新手,如果它以未经授权的错误响应,我需要重新尝试将项目提取到 api。 I need to call refresh token endpoint and then retry fetching.我需要调用刷新令牌端点,然后重试获取。 How can I do it using epics from redux-observable?如何使用来自 redux-observable 的史诗来做到这一点? I guess it should looks like this code, but I'm not sure what to do in catchError block.我想它应该看起来像这段代码,但我不确定在 catchError 块中做什么。

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您可以尝试像这样添加 pipe

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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