Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does forkjoin return results in a particular order?

Tags:

angular

rxjs

My app will make multiple parallel http call. Its based on a switch-fall though logic, so the number of query will vary (1, 2, or 3 calls). Is there a way for me to know which call gets returned first? I.e either assign a key or does it return in an ordered way based on the http request on the list?

Either way works for me. I just need to find a way to match the right result with the right call.

like image 929
Julian Avatar asked Nov 14 '17 09:11

Julian


People also ask

How does forkJoin work in angular?

forkJoin accepts a variable number of observables and subscribes to them in parallel. When all of the provided observables complete, forkJoin collects the last emitted value from each and emits them as an array (this is by default, more on this later).

What happens if one request fails in forkJoin?

“If any input observable errors at some point, forkJoin will error as well and all other observables will be immediately unsubscribed.”

Is forkJoin parallel?

Yes, forkJoin will subscribe to observables in parallel, which means the requests will be executed in parallel.

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

Yes it returns the results in the same order that you make requests in an array

example,

const bothrequests= Observable.combineLatest(
  this.http.get('https://testdb1.com/.json').map((res: Response) => res.json()),
  this.http.get('https://testdb2.com/.json').map((res: Response) => res.json())
)
bothrequests.subscribe(latestValues => {

});
like image 189
Sajeetharan Avatar answered Oct 06 '22 10:10

Sajeetharan