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

最新更新