Redux observable 重试不会重新发送 API 调用
Redux observable retry does not resend the API call
我正在使用 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-call。
我正在使用 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-call。