Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine the results of two observable in angular?

How to combine the results of two observable in angular?

this.http.get(url1)     .map((res: Response) => res.json())     .subscribe((data1: any) => {         this.data1 = data1;     });  this.http.get(url2)     .map((res: Response) => res.json())     .subscribe((data2: any) => {         this.data2 = data2;     });  toDisplay(){   // logic about combining this.data1 and this.data2; } 

The above is wrong, because we couldn't get data1 and data2 immediately.

this.http.get(url1)     .map((res: Response) => res.json())     .subscribe((data1: any) => {     this.http.get(url2)         .map((res: Response) => res.json())         .subscribe((data2: any) => {             this.data2 = data2;              // logic about combining this.data1 and this.data2             // and set to this.data;             this.toDisplay();         });     });  toDisplay(){   // display data   // this.data; } 

I can combine the results in the subscribe method of the second observable. But I'm not sure if it's a good practice to achieve my requirement.

Update:
Another way I found is using forkJoin to combine the results and return a new observable.

let o1: Observable<any> = this.http.get(url1)     .map((res: Response) => res.json())  let o2: Observable<any> = this.http.get(url2)     .map((res: Response) => res.json());  Observable.forkJoin(o1, o2)   .subscribe(val => {  // [data1, data2]     // logic about combining data1 and data2;     toDisplay(); // display data });  toDisplay(){   //  } 
like image 813
niaomingjian Avatar asked Aug 05 '17 04:08

niaomingjian


People also ask

How do I combine two observables?

The RxJS merge() operator is a join operator that is used to turn multiple observables into a single observable. It creates an output Observable, which concurrently emits all values from every given input Observables.

How do I subscribe to multiple observables?

In our component, we use forkJoin to combine the Observables into a single value Observable. The forkJoin operator will subscribe to each Observable passed into it. Once it receives a value from all the Observables, it will emit a new value with the combined values of each Observable.

What is the difference between combineLatest and forkJoin?

forkJoin - When all observables complete, emit the last emitted value from each. combineLatest - When any observable emits a value, emit the latest value from each.


1 Answers

Edit 2021: here is an updated tutorial:

https://scotch.io/tutorials/rxjs-operators-for-dummies-forkjoin-zip-combinelatest-withlatestfrom/amp

A great way to do this is to use the rxjs forkjoin operator (which is included with Angular btw), this keeps you away from nested async function hell where you have to nest function after function using the callbacks.

Here's a great tutorial on how to use forkjoin (and more):

https://coryrylan.com/blog/angular-multiple-http-requests-with-rxjs

In the example you make two http requests and then in the subscribe fat arrow function the response is an array of the results that you can then bring together as you see fit:

let character = this.http.get('https://swapi.co/api/people/1').map(res => res.json()); let characterHomeworld = this.http.get('http://swapi.co/api/planets/1').map(res => res.json());  Observable.forkJoin([character, characterHomeworld]).subscribe(results => {   // results[0] is our character   // results[1] is our character homeworld   results[0].homeworld = results[1];   this.loadedCharacter = results[0]; }); 

The first element in the array always corresponds to the first http request you pass in, and so on. I used this successfully a few days ago with four simultaneous requests and it worked perfectly.

like image 125
Graham Avatar answered Oct 05 '22 22:10

Graham