Hey there I've an update function call with nested subscribe, interdependent, just that on the success of the parent call subscribe the inner one
updateSelectedScopes() {
let params: any = {
userInterface: this.userInterface,
targetId: this.assessmentId,
targetUserId: this.currentUser['userId'],
};
this.lockService.acquireLock(params).subscribe(lockingDetails => {
if (lockingDetails) {
let payload = this.consolidatedPayload
this.assessmentService.updateSelectedScopes(payload).subscribe(res => {
if (res) {
this.releaseLock(
this.assessmentId,
this.currentUser['userId'],
this.userInterface
);
this.modalService.dismissAll();
if ((this.assessmentStatus === this.appConstants.CERTIFIED ||
this.assessmentStatus === this.appConstants.UN_CERTIFIED ) ||
this.assessmentStatus === this.appConstants.ASSESSMENT_FLAGGED) {
this.assessmentService.initiateWorkflow(this.assessmentId).pipe(take(1)).subscribe(() => {
this.router.navigate([this.appConstants.VIEW_ASSESSMENT_URI], { skipLocationChange: true, queryParams: { 'assessmentId': this.assessmentId, assessment: this.assessmentDetailsObj['assessmentName'], 'updateFlag': true } });
}, (error) => {
this.modalService.dismissAll();
this.openAlert(error.error);
});
} else {
this.router.navigate([this.appConstants.VIEW_ASSESSMENT_URI], { skipLocationChange: true, queryParams: { 'assessmentId': this.assessmentId, assessment: this.assessmentDetailsObj['assessmentName'], 'updateFlag': true } });
}
}
}, error => {
this.modalService.dismissAll();
this.releaseLock(
this.assessmentId,
this.currentUser['userId'],
this.userInterface
);
this.openAlert(error.error);
});
}
}, error => {
this.openLockDialog(
error.error,
this.currentUser['userId'],
this.assessmentId,
);
})
}
Can I optimize this function(with nested subscribes) with switchMap/mergeMap/flatMap
, in order to avoid this ugly nesting subscribes. Any help would be great