Reference

RxJS

    RxJS

    merge

    merge combines a number of observables streams and concurrently emits all values from every given input stream. As values from any combined sequence are produced, those values are emitted as part of the resulting sequence. Such process is often referred to as flattening in documentation.

    Use this operator if you’re not concerned with the order of emissions and is simply interested in all values coming out from multiple combined streams as if they were produced by one stream.

    The operator works in the following way:

    1. Subscribe to all source observables
    2. When a new value arrives from a source observable, pass it down to an observer
    3. Only after all source observables complete, send the complete notification to the observer.
    4. If any of the source observables throws an error, send the error notification to the observer.

    In the diagram below you can see the merge operator combining two streams A and B each producing 3 items and the values falling through to the resulting sequence as they occur:

    Progress: NaN%

    Usage
    Link to this section

    Use this operator if you’re not concerned with the order of emissions and is simply interested in all values coming out from multiple combined streams as if they were produced by one stream.

    Here’s an example of using merge on multiple streams:

    <>Copy
    const a = interval(500).pipe(map((v) => 'a' + v), take(3)); const b = interval(500).pipe(map((v) => 'b' + v), take(3)); merge(a, b).subscribe((value) => console.log(value));

    Playground
    Link to this section

    Additional resources
    Link to this section

    See also
    Link to this section

    Next

    Introduction