Multiple Observable results get in Single Subscriber like Promise.all() Angular: rxjs
Suppose we need to hit multiple APIS and need their response in a single success so how to do that while using Observable pattern. First, let's take an example of promise -
Observable Way
Now if we are using angular and using HttpClient so -
this.http.get() : returns Observable . So if we have multiple API’s calls we want when all API got success then response capture.
we need to import forkJoin
import { Observable, forkJoin } from ‘rxjs’;
import { HttpClient } from ‘@angular/common/http’;
Steps:
- Create a function and its return Type should be Array Of Observable
requestDataFromMultipleSources(): Observable<any[]> {….}
2. return forkJoin( /* array of all Observables */)
3. Subscribe this method: requestDataFromMultipleSources().subscribe(res => console.log(res) )
Thanks for reading. Please give a clap if you like this post. Give your comments below.