RxJS provides a function called forkJoin
. It allows you to input multiple Observables
and wait for all of them to finish. I am wondering if the resulting array will contain the results in the same order as the order of the input observables. If it wil not, which one of the operators does maintain the same order? I've been looking into the docs and was not able to find the answer.
It's worth noting that the forkJoin operator will preserve the order of inner observables regardless of when they complete.
Instead, return the forkJoin and in your component, subscribe to it. Please, not subscribe in service, if you want to store the result in a variable of service use pipe(tap(res=>{this. variable=res})). Normally you use the variable of subscribe, but "tap" is just after subscribe.
ForkJoin method signature has changedThis is now deprecated and you must pass an array of observables to forkJoin operator.
forkJoin is an operator that takes any number of input observables which can be passed either as an array or a dictionary of input observables. If no input observables are provided (e.g. an empty array is passed), then the resulting stream will complete immediately.
It will return results in the same order. As is described in these official docs.
Good to mention that it will emit only latest values of the streams:
var source = Rx.Observable.forkJoin(
Rx.Observable.of(1,2,3),
Rx.Observable.of(4)
);
source.subscribe(x => console.log("Result", x));
// LOG: Result [3,4]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With