我是RxJS的新手,如果api响应了未授权的错误,我需要重试获取项目。我需要调用刷新令牌端点,然后重试获取。我如何使用redux-observable中的epics来实现它?我想它应该像这样的代码,但我不知道在 catchError 块中该怎么做。
export const getItemsEpic = (action$) =>
action$.pipe(
ofType('FETCH_ITEMS'),
switchMap(() => getItemsRequest()),
map((response) => addItemsAction(response.data)),
catchError(err => {
???
}),
);
你可以尝试添加这样的管道
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.
);