DAY 4
1
Modern Web

多重資料流的 Operators

concat

``````var timer = Rx.Observable.interval(1000).take(4);
var sequence = Rx.Observable.from(['a', 'b', 'c','d']);

var result = timer.concat(sequence);
result.subscribe(x => console.log(x));
``````

``````var timer = Rx.Observable.interval(1000).take(4);
var sequence = Rx.Observable.from(['a', 'b', 'c','d']);

var result = sequence.concat(timer);
result.subscribe(x => console.log(x));
``````

merge

``````var timer = Rx.Observable.interval(1000).take(4);
var sequence = Rx.Observable.from(['a', 'b', 'c','d']);

var result = timer.merge(sequence);
result.subscribe(x => console.log(x));
``````

combineLatest

combineLatest 處理當兩個 Observable 資料都有變動時，拿最後的資料來產生一個新的 Observable

``````var timer = Rx.Observable.interval(1000).take(4);
var sequence = Rx.Observable.from(['a', 'b', 'c','d']);

var result = timer.combineLatest(sequence, (t,s) => ({t, s}));
result.subscribe(x => console.log(x));
``````

zip

zip 也就是處理當兩個 Observable 的資料有所變動時，如果有時間差，它會將值保留，再將他們一一配對產生新的 Observable

``````var timer = Rx.Observable.interval(1000).take(4);
var sequence = Rx.Observable.from(['a', 'b', 'c','d']);

var result = timer.zip(sequence, (t,s) => ({t, s}));
result.subscribe(x => console.log(x));
``````

forkJoin

``````var timer = Rx.Observable.interval(1000).take(4);
var sequence = Rx.Observable.from(['a', 'b', 'c','d']);

var result = Rx.Observable.forkJoin(timer, sequence);
result.subscribe(x => console.log(x));
``````