Redux可观察的重试不会重新发送API调用

问题描述

我正在使用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)
);

解决方法

将重试移至内部可观察的位置,如下所示:

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调用。