[英]How to use rxjs for multiple inner nested subscribes
在下面的代碼中,我在 subscribe 中使用了 subscribe。 此代碼有效,但代碼結構非常糟糕。 我想使用 rxjs( forkjoin 或 mergemap )重構此代碼。我不確定如何實現這一點。 有人可以幫我弄這個嗎? 任何幫助表示贊賞。
this.chapservice.cycle().subscribe((current) => {
this.selectedCycleId = current.ratingCycleId;
this.chapService
.getChapterEvalWithSkills(
this.activeUser.ipn,
this.selectedRatingCycleId
)
.subscribe((response) => {
console.log("response", response);
if (response && response.length > 0) {
this.chapterEvals = response;
}
});
this.chapservice
.getIsitEmployeeStatus(this.activeUser.ipn, this.selectedCycleId)
.subscribe((sdpStatus) => {
this.activeUserStatus = sdpStatus;
if (this.activeUserStatus.statusDescription == 'SUBMITTED') {
//do something
}
});
});
您必須使用forkJoin
和switchMap
組合。
switchMap
? - 因為您具有只能通過順序執行來實現的依賴值。forkJoin
? - 您可以在此處使用forkJoin
或mergeMap
。代碼
this.chapservice.cycle().pipe(
switchMap((current: any) =>
forkJoin(
this.chapService.getChapterEvalWithSkills(
this.activeUser.ipn,
this.selectedRatingCycleId
),
this.chapservice.getIsitEmployeeStatus(this.activeUser.ipn, current.ratingCycleId)
)
)
).subscribe(([chapterEvals, sdpStatus]) => {
console.log(chapterEvals, sdpStatus);
if (chapterEvals && chapterEvals.length > 0) {
this.chapterEvals = chapterEvals;
}
this.activeUserStatus = sdpStatus;
if (this.activeUserStatus.statusDescription == 'SUBMITTED') {
//do something
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.