merge

연산자(operator) 정의: merge(input: Observable): Observable

Turn multiple observables into a single observable.


:bulb: This operator can be used as either a static or instance method!

:bulb: If order not throughput is a primary concern, try concat instead!


Examples

Example 1: merging multiple observables, static method

( jsBin | jsFiddle )

//emit every 2.5 seconds
const first = Rx.Observable.interval(2500);
//emit every 2 seconds
const second = Rx.Observable.interval(2000);
//emit every 1.5 seconds
const third = Rx.Observable.interval(1500);
//emit every 1 second
const fourth = Rx.Observable.interval(1000);

//emit outputs from one observable
const example = Rx.Observable.merge(
  first.mapTo('FIRST!'),
  second.mapTo('SECOND!'),
  third.mapTo('THIRD'),
  fourth.mapTo('FOURTH')
);
//output: "FOURTH", "THIRD", "SECOND!", "FOURTH", "FIRST!", "THIRD", "FOURTH"
const subscribe = example.subscribe(val => console.log(val));
Example 2: merge 2 observables, instance method

( jsBin | jsFiddle )

//emit every 2.5 seconds
const first = Rx.Observable.interval(2500);
//emit every 1 second
const second = Rx.Observable.interval(1000);
//used as instance method
const example = first.merge(second);
//output: 0,1,0,2....
const subscribe = example.subscribe(val => console.log(val));

Additional Resources


:file_folder: Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/operator/merge.ts

results matching ""

    No results matching ""