Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will RxJS forkJoin return results in order?

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.

like image 941
Wouter Avatar asked Mar 20 '18 14:03

Wouter


People also ask

Does forkJoin maintain order?

It's worth noting that the forkJoin operator will preserve the order of inner observables regardless of when they complete.

How do I return data from forkJoin?

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.

Is forkJoin deprecated?

ForkJoin method signature has changedThis is now deprecated and you must pass an array of observables to forkJoin operator.

What does a forkJoin do?

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.


1 Answers

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]
like image 111
DDRamone Avatar answered Sep 21 '22 08:09

DDRamone