Reference

RxJS

    RxJS

    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:

    Progress: NaN%

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

    <>Copy
    const a = stream('a', 200, 3); const b = stream('b', 500, 3); forkJoin(a, b).subscribe(fullObserver(operator));

    And the editable demo:

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

    Additional resources
    Link to this section

    See also
    Link to this section

    Next

    Introduction