【问题标题】:Redux observable retry does not resend the API callRedux observable retry 不会重新发送 API 调用
【发布时间】:2021-01-24 10:16:46
【问题描述】:

我正在使用 redux-observable,并且希望在 API 调用抛出错误时重试 3 次。

但它不会重试,它只发送了一个http请求。

我编写了一个调用 github 用户 api 来查找用户的示例,如果您提供不存在的用户名,例如 This doesn't exist,那么它将引发 404 错误。我添加了retry(3),但它不会重试。

您可以在codesandbox找到代码

export const fetchUserEpic = action$ => action$.pipe(
  ofType(FETCH_USER),
  mergeMap(action =>
    ajax.getJSON(`https://api.github.com/users/${action.payload}`).pipe(
      map(response => fetchUserFulfilled(response))
    )
  ),
  retry(3)
);

【问题讨论】:

    标签: redux react-redux rxjs redux-observable


    【解决方案1】:

    将您的重试移到内部可观察对象中,如下所示:

    export const fetchUserEpic = action$ => action$.pipe(
      ofType(FETCH_USER),
      mergeMap(action =>
        ajax.getJSON(`https://api.github.com/users/${action.payload}`).pipe(
          map(response => fetchUserFulfilled(response)),
          retry(3)
        )
      )
    );
    

    您的 action$ 实际上并没有失败,而是您要重试的 ajax 调用。

    【讨论】:

      猜你喜欢
      • 2017-03-17
      • 2018-03-31
      • 2020-03-13
      • 2017-06-09
      • 2019-02-09
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多