0
Consider the following class:
export class TesteComponent {
//...
public onSubmit() {
this.checkA();
}
private checkA(): void {
this.service.checkA(value)
.subscribe(response => {
if(response) {
//regra de negócio
this.checkB();
} else {
//error
}
});
}
private checkB(): void {
this.service.checkB(value)
.subscribe(response => {
if(response) {
//regra de negócio
this.checkC();
} else {
//error
}
});
}
private checkC(): void {
this.service.checkC(value)
.subscribe(response => {
if(response) {
//regra de negócio
//fim
} else {
//error
}
});
}
}
Note that the methods checkA
, checkB
and checkC
perform a service that returns a Obervable
(we can see this through .subscribe
) which in turn is responsible for consuming a REST API (not explicit, but only that the service is asynchronous).
The chain of events begins with the method checkA
and when it is completed executes the method checkB
, which in turn executes the checkC
.
The point is that the 3 methods have well defined functions, but when completing the task the method performs another method that has a completely different responsibility, breaking the principle of single responsibility.
For example, when I get a car running, I don’t expect it to start accelerating on its own after putting on the seat belt; that’s basically what I’m doing when performing methods with different responsibilities.
I think it would be right for there to be a controlling element responsible for deciding which flow to follow (just as I decide what to do next after putting on my seat belt), but since each method is asynchronous, I don’t know how to solve the problem with Angular 5.
How to solve this problem?