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:
- Subscribe to all input observables
- When a source observable emits a value, override the value in the cache corresponding to this observable
- When a source observable completes, check if other observables completed
- Only after all source observables complete, emit the values from each source observables as a group
- Send the complete notification to the observer.
- 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
B. As soon as a corresponding pair is matched the resulting sequence produces a combined value:
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:
UsageLink to this section
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.