Our weekly RxJs operator is forkJoin
. This operator can be applied to any number of Observables. When all these Observables complete, forkJoin
emits the last value from each one of them.
A common use case for forkJoin
is when we need to trigger several HTTP requests in parallel with the HttpClient
then receive all of the responses at once in an array of responses:
The critical feature here is that those requests happen simultaneously, not one after the other (better performance), yet we receive the data when both have completed, which is convenient.
Another interesting syntax for forkJoin
is to pass an object instead of an array as follows:
Here is a complete example of forkJoin
in action.
As a reminder, my 2-hour RxJS workshop from ng-conf 2022 is also available on Youtube for free!