angular - 在 rxjs Angular 7 中重试后在哪里执行代码
问题描述
我正在使用 HTTP 拦截器,如果在重试后 refreshToken API 失败,我想注销我的用户。
return this.authService.refreshToken().pipe(
switchMap((token: any) => {
console.log(token);
this.isRefreshing = false;
this.refreshTokenSubject.next(token.accessToken);
return next.handle(this.addToken(req, token.accessToken));
}),
retry(3), //Tries the refreshToken() API if it returns with an error.
catchError(error => {
//If the refreshToken() API never returns a successful response, I want to Logout.
this.authService.Logout();
return throwError(error);
}
);
我的问题是,我正确地执行了 catchError 吗?我只希望它在重试 3 次但仍然失败后发生,但我担心它会在重试之前捕获错误。
解决方案
是的,它应该只在retry
. 此外,您可以不订阅错误回调,catchError
而是订阅错误回调。这是您的用例的模拟:https ://stackblitz.com/edit/typescript-kbfkq7
第一次订阅失败了几次,但低于retry
点 - 所以它解决了。
另一个在上面 - 所以catchError
会发出错误回调。
推荐阅读
- react-native - 我的 React-Native 项目-Android 中未加载字体
- javascript - 在 html 5 画布上添加背景图像
- sql - 根据 sys.tables 中的名称声明一个变量,然后在动态 SQL 中删除基于该变量的表
- javascript - 将字符串更改为日期的问题
- python - 循环似乎正在跳过网址
- linear-regression - 我是否正确考虑了线性回归模型?
- javascript - 如何在反应中一键更改所有图像
- swift - 如何为线条设置动画以使其变大?
- javascript - 当 Class 的构造函数在 JavaScript 中有 fetch() 时,如何启动和使用 Class 实例?
- azure-devops - 如何将类库项目中的文件和测试程序集包含到已发布的工件中