How can I execute observable methods that depends on the sequence in a clear way in typescript?
example:
Init(){
this.methodThatUseObservable();
this.methodThatShouldExecuteAfter();
this.anotherMethod();
... (continue)
}
And in each of those methods I have inside an obsevable method and subscribe.
methodThatUseObservable(){
this.service1.get().subscribe(
(value1) => {
this.value1 = value1;
}
)
}
methodThatShouldExecuteAfter(){
this.service2.get(this.value1).subscribe( //depends on the first
(value2) => {
this.value2 = value2;
}
)
}
anotherMethod(){
this.service3.get(this.value2).subscribe( //depends on the second
(value3) => {
this.value3 = value3;
}
)
}
...(so on)
Of cource I could do:
Init(){
this.methodThatUseObservable();
}
And in each of those methods other methods that depends:
methodThatUseObservable(){
this.service1.get().subscribe(
(value1) => {
this.value1 = value1;
this.methodThatShouldExecuteAfter();
}
)
}
methodThatShouldExecuteAfter(){
this.service2.get(this.value1).subscribe( //depends on the first
(value2) => {
this.value2 = value2;
this.anotherMethod();
}
)
}
anotherMethod(){
this.service3.get(this.value2).subscribe( //depends on the second
(value3) => {
this.value3 = value3;
...(and so on)
}
)
}
But I don't feel it very clear, in this way I think the sequence is not very clear.
Maybe the solution is returning a promize and executing then method?
this.methodThatUseObservable().then(()=>{
this.methodThatShouldExecuteAfter();
}).then(()=>{
this.anotherMethod();
})
I don't know if such changes in the structure is possible, please help.