ForkJoin

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 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.

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

Last updated