forkJoin
Joins last values emitted by passed Observables.
Parameters
sources |
Any number of Observables provided either as an array or as an arguments passed directly to the operator. |
Returns
Observable<T[]>
: Observable emitting either an array of last values emitted by passed Observables
or value from project function.
Description
Wait for Observables to complete and then combine last values they emitted.
forkJoin
is an operator that takes any number of Observables which can be passed either as an array
or directly as arguments. If no input Observables are provided, resulting stream will complete
immediately.
forkJoin
will wait for all passed Observables to complete and then it will emit an array with last
values from corresponding Observables. So if you pass n
Observables to the operator, resulting
array will have n
values, where first value is the last thing emitted by the first Observable,
second value is the last thing emitted by the second Observable and so on. That means forkJoin
will
not emit more than once and it will complete after that. If you need to emit combined values not only
at the end of lifecycle of passed Observables, but also throughout it, try out combineLatest
or zip
instead.
In order for resulting array to have the same length as the number of input Observables, whenever any of
that Observables completes without emitting any value, forkJoin
will complete at that moment as well
and it will not emit anything either, even if it already has some last values from other Observables.
Conversely, if there is an Observable that never completes, forkJoin
will never complete as well,
unless at any point some other Observable completes without emitting value, which brings us back to
the previous case. Overall, in order for forkJoin
to emit a value, all Observables passed as arguments
have to emit something at least once and complete.
If any input Observable errors at some point, forkJoin
will error as well and all other Observables
will be immediately unsubscribed.
Optionally forkJoin
accepts project function, that will be called with values which normally
would land in emitted array. Whatever is returned by project function, will appear in output
Observable instead. This means that default project can be thought of as a function that takes
all its arguments and puts them into an array. Note that project function will be called only
when output Observable is supposed to emit a result.