24 Jul 2024
2 min

forkJoin – RxJS Reference

forkJoin

forkJoin takes a number of input observables and waits for all passed observables to complete. Once they are complete, it will then emit a group of the last values from corresponding observables.

The resulting stream emits only one time when all of the inner streams complete. It will never complete if any of the inner streams doesn’t complete and will throw an error if any of the inner streams errors out.

The operator works in the following way:

  1. Subscribe to all input observables
  2. When a source observable emits a value, override the value in the cache corresponding to this observable
  3. When a source observable completes, check if other observables completed
  4. Only after all source observables complete, emit the values from each source observables as a group
  5. Send the complete notification to the observer.
  6. If any of the source observables throws an error, send the error notification to the observer.

In the diagram below you can see the forkJoin operator combining two streams A and B. As soon as a corresponding pair is matched the resulting sequence produces a combined value:

forkJoin operator diagram

Here is the code example that demonstrates the setup shown by the above diagram:

const a = stream('a', 200, 3);
const b = stream('b', 500, 3);

forkJoin(a, b).subscribe(fullObserver(operator));

And the editable demo:

Usage

This operator can be used when there’s a group of streams and only care about the final emitted value of each. Often such sequences have only a single emission. For example, you may want to make multiples network requests and only want to take an action when a response has been received for all of them. It is in some way similar to Promise.all functionality. However, if you have a stream that emits more than one item, those items will be ignored except for the last value.

Additional resources

See also

Frequently asked questions

How do I wait for multiple observables to complete before emitting in RxJS?

Use forkJoin() from RxJS to combine several observables so you receive a single emission only after all of them have completed. forkJoin subscribes to every input, caches the latest value from each, and emits a group of those last values once every source completes. It suits cases where you only care about the final result of each stream, such as several network requests that must all return before you act.

How is forkJoin different from Promise.all?

forkJoin works much like Promise.all, since both wait for every input to finish and then deliver the results together. The key difference is how multi-emitting sources are handled: forkJoin ignores every value except the last one from each observable. Use forkJoin when each source emits a single value or when only the final value of each stream matters.

What happens if one observable passed to forkJoin never completes?

The resulting stream never emits and never completes if any inner observable fails to complete. forkJoin waits for all sources to complete before emitting, so a long-lived source such as an open-ended stream will block the output indefinitely. Before passing a source into forkJoin, confirm that it will finish on its own.

How does forkJoin handle errors from inner observables?

forkJoin sends an error notification to the observer as soon as any source observable throws an error. That error travels through the resulting stream, so subscribers should provide an error callback in subscribe() to handle it. Because a single failing source fails the whole combined stream, errors in one request will prevent the group result from arriving.

Why does forkJoin only emit the last value from each stream?

forkJoin caches the most recent value from each source and overrides it whenever that source emits again. It emits the cached values as a group only after all sources complete, so every earlier item is discarded. This makes forkJoin a poor fit for continuous streams like user input events, where nothing reaches the observer until all sources have finished.

Share this post

Sign up for our newsletter

Stay up-to-date with the trends and be a part of a thriving community.