I'm new to RXJS and I have a working piece of code for http-request and subscribing to Observables from a shared service in Angular. This works, but I'd want to get rid of the nested subscriptions, which I know is probably very possible with RXJS operators, but I haven't figured out how. The usage of flatMap
in the nested request wouldn't be necessary, as the second nested request isn't dependable on the first.
this.sub = this.service.isPending$
.subscribe(data => {
if(data) { // gives boolean
this.service.getPending()
.subscribe((data:Offer) => this.offer = data)
} else {
this.sub2 = this.service.amount$
.flatMap((data: Amount) => {
this.amount = data;
return this.service.getOffer()
})
.subscribe((data: Offer) => {
this.offer = data;
})
}
})
How would I go about cleaning this code up? I'm using RXJS: "^5.0.1"