I need to make all requests in parallel whenever it's possible. Here is the simplified version of my code:
const arr = [
'https://jsonplaceholder.typicode.com/users/1',
'https://jsonplaceholder.typicode.com/users/2',
'https://jsonplaceholder.typicode.com/users/3',
];
let observables = [];
for (let url of arr) {
observables.push(this.http.get(url));
}
const obs$ = this.http.get('http://localhost:3000/users').pipe(
map((data: any) => {
//an array of urls like https://jsonplaceholder.typicode.com/users/5
let urls = data.data;
let observables = [];
for (let url of urls) {
observables.push(this.http.get(url));
}
return observables;
}),
mergeMap((data) => forkJoin([...data]))
);
forkJoin([...observables, obs$]).subscribe({
next: (data) => console.log(data, 'from fork'),
error:(err)=>console.log(err)
});
So, I have in this case:
- An array of observables
observables
- An observable
obs$
that fetches some urls, and upon fetching the urls, I need to make requests to those urls in parallel as well.
I don't care about results, I only care if something errors out. These example code seems to be working properly, however, I've got a couple of questions since I'm trying to wrap my head around RxJS.
- Is this a proper way of doing things in order to achieve goals I described above?
mergeMap((data) => forkJoin([...data]))
-> mergeMap here basically only helps me getting an Observable out of the nested Observable, right? So I could technically use concatMap or switchMap with the same effect?