【发布时间】:2021-12-01 19:38:53
【问题描述】:
我有代码,检查状态,如果状态不是Completed 或Failed,重试调用
这里是代码
getRecognitionById() {
this.loaderService.show(null, true);
this.vendorWebApiService
.createRecognition(this.executiveChangeId)
.pipe(take(1))
.subscribe((res) => {
this.refresher$ = interval(5000);
this.refreshSub = this.refresher$.subscribe(() => {
this.checkStatus(res.taskRequestId);
if (this.jobStatus === "Completed") {
this.refreshSub.unsubscribe();
this.getLatestFeedback();
this.loaderService.hide(true);
}
if (this.jobStatus === "Failed") {
this.loaderService.hide(true);
this.refreshSub.unsubscribe();
alert("Recognition failed. Try again later");
} else {
if (this.checkCount < 36) {
this.checkStatus(res.taskRequestId); //run check status again only when we get response
} else {
this.loaderService.hide(true);
this.refreshSub.unsubscribe();
alert("Recognition failed. Try again later");
}
}
});
});
}
我需要等待来自checkStatus() 方法的响应,然后才能再次运行checkStatus()(我注释了需要实现此逻辑的部分代码)
这是checkStatus方法的代码
checkStatus(taskRequestId: number) {
this.checkCount++;
this.vendorWebApiService
.getRecognition(taskRequestId, this.executiveChangeId)
.pipe(take(1))
.subscribe((recognitionResponse) => {
this.jobStatus = recognitionResponse.jobStatus;
if (recognitionResponse.jobStatus === "Completed") {
this.recognitionData = recognitionResponse;
}
});
}
我怎样才能做到这一点?
【问题讨论】:
-
在这里使用像 switchMap() 这样的 rxjs 管道运算符很好。
-
你能说明我需要如何以及在哪里使用它吗? @GaurangDhorda
-
先用stackblitz做这个代码demo,这样对理解更有帮助
标签: javascript angular typescript rxjs