我不知道是否有更简单的方法,但我会使用concatMap()运算符并递归调用deleteDocument()
使用上次调用返回的 id:
这应该模拟您的情况:
import {Observable} from 'rxjs';
function deleteDocument(willFail: bool, id) {
return Observable.of("I'm your response: " + id)
// process response and eventualy throw error
.concatMap(val => {
if (willFail) {
return Observable.throw({response: val, message: "It's broken"});
} else {
return Observable.of(val);
}
})
// recover from the error
.catch(initialError => {
return deleteDocument(false, initialError.response.length);
});
}
deleteDocument(true, 42).subscribe(result => console.log(result));
观看现场演示:http://plnkr.co/edit/rUbLgEwtw7lSBueKJ5HN?p=preview
这会打印到控制台:
I'm your response: 21
我正在模拟错误willFail
争论。操作员concatMap
服务器在这里区分错误和正确响应。我也把原件传上来id
以及一些仅用于演示目的的处理。
在你的情况下,你会附加到intercept()
还有重建原始 HTTP 请求所需的所有参数,而不是this.getTicket().do()
use:
return this.getTicket().concatMap(response => {
var newBody = {id: response.ticketId};
return this.post(originalUrl, newBody, ...);
});
操作员do()
对于调试操作符链中发生的情况最有用,它不会以任何方式修改链。
我不知道你的代码实际上做了什么,但我希望这对你有意义。
也许和你有类似的问题:Observable 继续调用 API 并根据条件更改参数