I need to fill in three controls with hierarchic structure: companies, branches, employees. Out of user service I get currently logged in user, then via user's domain name I receive his full data as Employee, then per employee's branchId I obtain branch object, then I request all companies and set control values as currrent employee, current branch, and current branch's company.
I end up with nested tree of subscribers. How can I avoid this and refactor it to be more straightforward?
this.userService.getCurrentlyLoggedInUser().subscribe(
user => {
this.user = user;
this.getEmployee(this.user.domainName).subscribe(
employees => {
if (employees.length === 0) {
this.isLoading = false;
return;
}
this.getBranch(employees[0].branchId).subscribe(
branches => {
if (branches.length === 0) {
this.isLoading = false;
return;
}
this.odata.companies().subscribe(
companies => {
this.setDefaultValues(companies, branches[0], employees[0]);
this.isLoading = false;
},
error => this.isLoading = false
);
},
error => this.isLoading = false
);
},
error => this.isLoading = false
);
},
error => this.isLoading = false
);