【发布时间】:2021-12-09 13:36:55
【问题描述】:
我正在尝试根据条件执行休息调用。该调用将被执行,但根据条件,它将在另一个 rest 调用之后执行。 目前,我尝试过这种方式,但我确信这不是最好的方式:
if(!checkTokenValidation()) {
this.service.getToken().pipe(
map(response => {
setToken(response);
})
).subscribe(() => {
this.service.search().subscribe(data => {
...
})
})
} else {
this.service.search().subscribe(data => {
...
})
}
我需要在每种情况下都进行搜索,但如果令牌无效,我需要先获取新令牌。 有没有办法在没有冗余代码的情况下做到这一点? 谢谢
【问题讨论】:
-
如果你在你的应用中使用HTTPinterceptor来检查、验证和setToken会更好。