rxjs重新验证解决了
我正在实施一个呼叫服务器的角服务。在每个通话中,我都需要传递一个持续约一分钟的令牌,然后我需要绘制响应以获取特定字段。因此,如果我的主电话失败了,我需要再次致电以获得令牌,等待响应然后重试我的第一个电话,是否有一种简单的方法来执行此操作?这是我的两种方法(它们都不属于属性):
return this.http.post(url,
firstCallText(this.token), {
responseType: 'text',
headers
})
.pipe(
map((xmlString: string) => {
let asJson = this.xmlStringToJson(xmlString);
return asJson["soap:Envelope"]["soap:Body"]["Response"]["#text"];
}),
catchError(async err=>{
await this.http.post(url,
getToken(),
{
responseType: 'text',
headers
}).pipe(map((xmlString: string) => {
let asJson = this.xmlStringToJson(xmlString);
this.token = asJson["soap:Envelope"]["soap:Body"]["Response"]["Token"]["#text"];
})).toPromise()
return EMPTY
}),
retry()
)
第一种方法失败了,因为在收到新令牌之前,请重试()。 我的第二种方法是:
return this.http.post(url,
firstCallText(this.token), {
responseType: 'text',
headers
})
.pipe(
map((xmlString: string) => {
let asJson = this.xmlStringToJson(xmlString);
return asJson["soap:Envelope"]["soap:Body"]["Response"]["#text"];
}),
retryWhen((errors) =>{
this.http.post(url,
getToken(),
{
responseType: 'text',
headers
}).pipe(map((xmlString: string) => {
let asJson = this.xmlStringToJson(xmlString);
this.token = asJson["soap:Envelope"]["soap:Body"]["Response"]["Token"]["#text"];
})).toPromise()
return EMPTY
})
)
第二种方法无法正确重试,我不想设置延迟,因为令牌呼叫可能更短或更长。
I'm implementing an Angular service which calls a server for data. In every call I need to pass a token which lasts about a minute and then I need to map the response to get an specific field. So if my main call fails I need to call again for a token, wait for the response and then retry my first call, is there an easy way of doing this? Here are my two approaches (neither of them work propertly):
return this.http.post(url,
firstCallText(this.token), {
responseType: 'text',
headers
})
.pipe(
map((xmlString: string) => {
let asJson = this.xmlStringToJson(xmlString);
return asJson["soap:Envelope"]["soap:Body"]["Response"]["#text"];
}),
catchError(async err=>{
await this.http.post(url,
getToken(),
{
responseType: 'text',
headers
}).pipe(map((xmlString: string) => {
let asJson = this.xmlStringToJson(xmlString);
this.token = asJson["soap:Envelope"]["soap:Body"]["Response"]["Token"]["#text"];
})).toPromise()
return EMPTY
}),
retry()
)
This first method fails because the retry() gets called before the new token is received.
My second approach:
return this.http.post(url,
firstCallText(this.token), {
responseType: 'text',
headers
})
.pipe(
map((xmlString: string) => {
let asJson = this.xmlStringToJson(xmlString);
return asJson["soap:Envelope"]["soap:Body"]["Response"]["#text"];
}),
retryWhen((errors) =>{
this.http.post(url,
getToken(),
{
responseType: 'text',
headers
}).pipe(map((xmlString: string) => {
let asJson = this.xmlStringToJson(xmlString);
this.token = asJson["soap:Envelope"]["soap:Body"]["Response"]["Token"]["#text"];
})).toPromise()
return EMPTY
})
)
The second one doesn´t retry correctly, I don't want to set a delay because the token call might be shorter or longer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在这里面临的问题是
重试
模仿源而没有错误,您实际上想处理错误本身。因此,我的建议是将您的
catherror
opertaor扩展,以使其本身处理“失败”情况并恢复操作,以获取服务器的数据。这是一个伪代码解决方案
您可以在此处找到工作的伪代码解决方案
live xpemend xpemppect
The issue that you are facing here is that the
retry
mimics the source without it's errors, where you actually want to handle the error itself.So my suggestion is to extend your
catchError
opertaor in such a manner that it by itself handled the "failing" case and resumes the operation, for fetching data from the server.Here is a pseudo code solution
You can find working pseudo code solution here
Live example