Recently I’ve been working with Angular 2/4. One day I came across with something like this:
public getCustomersOnArea(zip:string):Observable<Customer>{..}
The problem was that we had to make 2 calls to get the data. Even more one call depended on the data fetched from the other. How to solve this? One way it’s to encapsulate the 2 calls into a third one and return that to be subscribed.
public getCustomersOn(zip:string):Observable<Customer>{
return new Observable<Customer>(subscriber => {
this.http.post(zip)
.subscribe(res => {
this.http.post(res)
.subscribe(r => {
subscriber.next(r);
},
e=> subscriber.error(e),
()=> subscriber.complete());
});
}
And that’s it. Do you know another way? Leave it in the comments section
