DEV Community

loading...

Promise.all alternative in RxJS

sainig profile image Gaurav Saini ・1 min read

I'm a bit late to the party but I finally took out the time to learn RxJS and already in love with it ❤️.

What I did

I was wondering, there has to be something like Promise.all in RxJS and here's what I came up with

import { ajax } from 'rxjs/ajax';
import { of } from 'rxjs';

const myObs = of([
  ajax('https://jsonplaceholder.typicode.com/users/1'),
  ajax('https://jsonplaceholder.typicode.com/posts/1')
]);
// now myObs is of the type Observable<Observable<AjaxResponse>[]>
// for some reason I don't feel too good about this

myObs.subscribe(function(value) {
  /**
   * here I get an array of 2 observables which I have to
   * loop over and subscribe to each of the observables
   */
  console.log('emitted value:', value);
})

What I need

So, my question is, is there any better way of doing what I'm trying to do?
Thanks a lot in advance!

Discussion (4)

pic
Editor guide
Collapse
glebirovich profile image
Gleb Irovich

combineLatest has a very similar signature
learnrxjs.io/learn-rxjs/operators/...

Collapse
kelvinmai profile image
Kelvin Mai

What you want is forkJoin

Collapse
gktim profile image
gkTim • Edited

You can use for forkJoin for this

Collapse
sainig profile image
Gaurav Saini Author

Thanks, that’s exactly what I needed 👍