[RxJS] Filtering operator: single, race

Single, race both get only one emit value from the stream.

Single(fn):

const source = Rx.Observable.from([1,2,3,4,5]);
var single = source.single( x => x === 3); /* (12345)| (source) single( x => x === 3)
3| (single) */ var sub = single.subscribe( x => console.log(x)); //

race(...observable): Observable

const winner = Rx.Observable.race(
//emit every 1.5s
Rx.Observable.interval(1500),
//emit every 1s
Rx.Observable.interval(1000).mapTo('1s won!'),
//emit every 2s
Rx.Observable.interval(2000),
//emit every 2.5s
Rx.Observable.interval(2500)
).take(1); /** ------0 (1500)
----0 (1000).mapTo('1s won!')
--------0 (2000)
----------0 (2500) race ----(1s won!)| (take(1)) */ const sub2 = winner.subscribe( x => console.log(x)); // 1s won!
上一篇:[javaSE] 看博客学习多线程的创建方式和优劣比较和PHP多线程


下一篇:dubbo spring pom文件报错:提示no declaration can be found for element 'dubbo:service'.