function
stable
Accepts an Array
of ObservableInput
or a dictionary Object
of ObservableInput
and returns an Observable
that emits either an array of values in the exact same order as the passed array, or a dictionary of values in the same shape as the passed dictionary.
forkJoin(...sources: any[]): Observable<any>
sources | Any number of Observables provided either as an array or as an arguments passed directly to the operator. |
Observable<any>
: Observable emitting either an array of last values emitted by passed Observables or value from project function.
Wait for Observables to complete and then combine last values they emitted.
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, resulting stream will complete immediately.
forkJoin
will wait for all passed observables to complete and then it will emit an array or an object with last values from corresponding observables.
If you pass an array of 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.
If you pass a dictionary of observables to the operator, resulting objects will have the same keys as the dictionary passed, with their last values they've emitted located at the corresponding key.
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.
import { forkJoin, of, timer } from 'rxjs'; const observable = forkJoin({ foo: of(1, 2, 3, 4), bar: Promise.resolve(8), baz: timer(4000), }); observable.subscribe({ next: value => console.log(value), complete: () => console.log('This is how it ends!'), }); // Logs: // { foo: 4, bar: 8, baz: 0 } after 4 seconds // "This is how it ends!" immediately after
import { forkJoin, of } from 'rxjs'; const observable = forkJoin([ of(1, 2, 3, 4), Promise.resolve(8), timer(4000), ]); observable.subscribe({ next: value => console.log(value), complete: () => console.log('This is how it ends!'), }); // Logs: // [4, 8, 0] after 4 seconds // "This is how it ends!" immediately after
forkJoin(v1: SubscribableOrPromise<T>): Observable<[T]>
v1 | Type: |
Observable<[T]>
forkJoin(v1: ObservableInput<T>, v2: ObservableInput<T2>): Observable<[T, T2]>
v1 | Type: |
v2 | Type: |
Observable<[T, T2]>
forkJoin(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>): Observable<[T, T2, T3]>
v1 | Type: |
v2 | Type: |
v3 | Type: |
Observable<[T, T2, T3]>
forkJoin(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>): Observable<[T, T2, T3, T4]>
v1 | Type: |
v2 | Type: |
v3 | Type: |
v4 | Type: |
Observable<[T, T2, T3, T4]>
forkJoin(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>): Observable<[T, T2, T3, T4, T5]>
v1 | Type: |
v2 | Type: |
v3 | Type: |
v4 | Type: |
v5 | Type: |
Observable<[T, T2, T3, T4, T5]>
forkJoin(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>): Observable<[T, T2, T3, T4, T5, T6]>
v1 | Type: |
v2 | Type: |
v3 | Type: |
v4 | Type: |
v5 | Type: |
v6 | Type: |
Observable<[T, T2, T3, T4, T5, T6]>
forkJoin(sources: [ObservableInput<A>]): Observable<[A]>
sources |
Observable<[A]>
forkJoin(sources: [ObservableInput<A>, ObservableInput<B>]): Observable<[A, B]>
sources | Type: |
Observable<[A, B]>
forkJoin(sources: [ObservableInput<A>, ObservableInput<B>, ObservableInput<C>]): Observable<[A, B, C]>
sources | Type: |
Observable<[A, B, C]>
forkJoin(sources: [ObservableInput<A>, ObservableInput<B>, ObservableInput<C>, ObservableInput<D>]): Observable<[A, B, C, D]>
sources | Type: |
Observable<[A, B, C, D]>
forkJoin(sources: [ObservableInput<A>, ObservableInput<B>, ObservableInput<C>, ObservableInput<D>, ObservableInput<E>]): Observable<[A, B, C, D, E]>
sources | Type: |
Observable<[A, B, C, D, E]>
forkJoin(sources: [ObservableInput<A>, ObservableInput<B>, ObservableInput<C>, ObservableInput<D>, ObservableInput<E>, ObservableInput<F>]): Observable<[A, B, C, D, E, F]>
sources | Type: |
Observable<[A, B, C, D, E, F]>
forkJoin(sources: A): Observable<ObservedValuesFromArray<A>[]>
sources | Type: |
Observable<ObservedValuesFromArray<A>[]>
forkJoin(sourcesObject: {
}): Observable<never>
sourcesObject | Type: |
Observable<never>
forkJoin(sourcesObject: T): Observable<{
[K in keyof T]: ObservedValueOf<T[K]>;
}>
sourcesObject | Type: |
Observable<{ [K in keyof T]: ObservedValueOf<T[K]>; }>
forkJoin(...args: Array<ObservableInput<any> | Function>): Observable<any>
args | Type: |
Observable<any>
forkJoin(...sources: ObservableInput<T>[]): Observable<T[]>
sources | Type: |
Observable<T[]>
© 2015–2018 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/index/function/forkJoin