Using Angular and the RxJS library.
I have a simple method:
method(){
const data = this.data;
const moreData = this.moreData;
this.anotherMethodOne(data)
this.anotherMethodTwo(moreData)
}
As you can see I am calling two other methods. anotherMethodOne()
should be executed before anotherMethodTwo()
- and it is.
Within anotherMethodOne()
I have a .subscribe
:
anotherMethodOne(data) {
<!-- at this point it jumps out of this method and continues to proceed in the parent method, but returns later -->
this.service.getService(id).subscribe(res => {
res
}
}
As soon as my debugger hits the .subscribe call it jumps back into the parent method()
and proceeds to execute this.anotherMethodTwo(moreData)
. It then jumps back into this.anotherMethodOne(data)
to finally complete the .subscribe
- however, by this time its too late.
I need to make sure this.anotherMethodOne(data)
is completed fully before this.anotherMethodTwo(moreData)
runs or ensure that the .subscribe runs before it proceeds any further.
I started looking at switchMap and concat in the RxJS library, but unsure how that would be implemented in my case.