W3cubDocs

/RxJS

merge

function stable

Creates an output Observable which concurrently emits all values from every given input Observable.

merge<T, R>(...observables: Array<ObservableInput<any> | SchedulerLike | number>): Observable<R>

Parameters

observables

Input Observables to merge together.

Returns

Observable<R>: an Observable that emits items that are the result of every input Observable.

Description

Flattens multiple Observables together by blending their values into one Observable.

merge marble diagram

merge subscribes to each given input Observable (as arguments), and simply forwards (without doing any transformation) all the values from all the input Observables to the output Observable. The output Observable only completes once all input Observables have completed. Any error delivered by an input Observable will be immediately emitted on the output Observable.

Examples

Merge together two Observables: 1s interval and clicks

import { merge, fromEvent, interval } from 'rxjs';

const clicks = fromEvent(document, 'click');
const timer = interval(1000);
const clicksOrTimer = merge(clicks, timer);
clicksOrTimer.subscribe(x => console.log(x));

// Results in the following:
// timer will emit ascending values, one every second(1000ms) to console
// clicks logs MouseEvents to console everytime the "document" is clicked
// Since the two streams are merged you see these happening
// as they occur.

Merge together 3 Observables, but only 2 run concurrently

import { merge, interval } from 'rxjs';
import { take } from 'rxjs/operators';

const timer1 = interval(1000).pipe(take(10));
const timer2 = interval(2000).pipe(take(6));
const timer3 = interval(500).pipe(take(10));
const concurrent = 2; // the argument
const merged = merge(timer1, timer2, timer3, concurrent);
merged.subscribe(x => console.log(x));

// Results in the following:
// - First timer1 and timer2 will run concurrently
// - timer1 will emit a value every 1000ms for 10 iterations
// - timer2 will emit a value every 2000ms for 6 iterations
// - after timer1 hits it's max iteration, timer2 will
//   continue, and timer3 will start to run concurrently with timer2
// - when timer2 hits it's max iteration it terminates, and
//   timer3 will continue to emit a value every 500ms until it is complete

Overloads

merge(v1: ObservableInput<T>, scheduler: SchedulerLike): Observable<T>

Parameters

v1

Type: ObservableInput.

scheduler

Type: SchedulerLike.

Returns

Observable<T>

merge(v1: ObservableInput<T>, concurrent: number, scheduler: SchedulerLike): Observable<T>

Parameters

v1

Type: ObservableInput.

concurrent

Type: number.

scheduler

Type: SchedulerLike.

Returns

Observable<T>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, scheduler: SchedulerLike): Observable<T | T2>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

scheduler

Type: SchedulerLike.

Returns

Observable<T | T2>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, concurrent: number, scheduler: SchedulerLike): Observable<T | T2>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

concurrent

Type: number.

scheduler

Type: SchedulerLike.

Returns

Observable<T | T2>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, scheduler: SchedulerLike): Observable<T | T2 | T3>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

scheduler

Type: SchedulerLike.

Returns

Observable<T | T2 | T3>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

concurrent

Type: number.

scheduler

Type: SchedulerLike.

Returns

Observable<T | T2 | T3>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

v4

Type: ObservableInput.

scheduler

Type: SchedulerLike.

Returns

Observable<T | T2 | T3 | T4>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

v4

Type: ObservableInput.

concurrent

Type: number.

scheduler

Type: SchedulerLike.

Returns

Observable<T | T2 | T3 | T4>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

v4

Type: ObservableInput.

v5

Type: ObservableInput.

scheduler

Type: SchedulerLike.

Returns

Observable<T | T2 | T3 | T4 | T5>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

v4

Type: ObservableInput.

v5

Type: ObservableInput.

concurrent

Type: number.

scheduler

Type: SchedulerLike.

Returns

Observable<T | T2 | T3 | T4 | T5>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5 | T6>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

v4

Type: ObservableInput.

v5

Type: ObservableInput.

v6

Type: ObservableInput.

scheduler

Type: SchedulerLike.

Returns

Observable<T | T2 | T3 | T4 | T5 | T6>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5 | T6>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

v4

Type: ObservableInput.

v5

Type: ObservableInput.

v6

Type: ObservableInput.

concurrent

Type: number.

scheduler

Type: SchedulerLike.

Returns

Observable<T | T2 | T3 | T4 | T5 | T6>

merge(v1: ObservableInput<T>): Observable<T>

Parameters

v1

Type: ObservableInput.

Returns

Observable<T>

merge(v1: ObservableInput<T>, concurrent?: number): Observable<T>

Parameters

v1

Type: ObservableInput.

concurrent

Optional. Default is undefined.

Type: number.

Returns

Observable<T>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>): Observable<T | T2>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

Returns

Observable<T | T2>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, concurrent?: number): Observable<T | T2>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

concurrent

Optional. Default is undefined.

Type: number.

Returns

Observable<T | T2>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>): Observable<T | T2 | T3>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

Returns

Observable<T | T2 | T3>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, concurrent?: number): Observable<T | T2 | T3>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

concurrent

Optional. Default is undefined.

Type: number.

Returns

Observable<T | T2 | T3>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>): Observable<T | T2 | T3 | T4>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

v4

Type: ObservableInput.

Returns

Observable<T | T2 | T3 | T4>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, concurrent?: number): Observable<T | T2 | T3 | T4>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

v4

Type: ObservableInput.

concurrent

Optional. Default is undefined.

Type: number.

Returns

Observable<T | T2 | T3 | T4>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>): Observable<T | T2 | T3 | T4 | T5>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

v4

Type: ObservableInput.

v5

Type: ObservableInput.

Returns

Observable<T | T2 | T3 | T4 | T5>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, concurrent?: number): Observable<T | T2 | T3 | T4 | T5>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

v4

Type: ObservableInput.

v5

Type: ObservableInput.

concurrent

Optional. Default is undefined.

Type: number.

Returns

Observable<T | T2 | T3 | T4 | T5>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>): Observable<T | T2 | T3 | T4 | T5 | T6>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

v4

Type: ObservableInput.

v5

Type: ObservableInput.

v6

Type: ObservableInput.

Returns

Observable<T | T2 | T3 | T4 | T5 | T6>

merge(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>, concurrent?: number): Observable<T | T2 | T3 | T4 | T5 | T6>

Parameters

v1

Type: ObservableInput.

v2

Type: ObservableInput.

v3

Type: ObservableInput.

v4

Type: ObservableInput.

v5

Type: ObservableInput.

v6

Type: ObservableInput.

concurrent

Optional. Default is undefined.

Type: number.

Returns

Observable<T | T2 | T3 | T4 | T5 | T6>

merge(...observables: (ObservableInput<T> | number)[]): Observable<T>

Parameters

observables

Type: (ObservableInput | number)[].

Returns

Observable<T>

merge(...observables: (ObservableInput<T> | SchedulerLike | number)[]): Observable<T>

Parameters

observables

Type: (ObservableInput | SchedulerLike | number)[].

Returns

Observable<T>

merge(...observables: (ObservableInput<any> | number)[]): Observable<R>

Parameters

observables

Type: (ObservableInput | number)[].

Returns

Observable<R>

merge(...observables: (ObservableInput<any> | SchedulerLike | number)[]): Observable<R>

Parameters

observables

Type: (ObservableInput | SchedulerLike | number)[].

Returns

Observable<R>

See Also

© 2015–2018 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/index/function/merge