[RxJS] Filtering operator: single, race

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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)); // 3

 

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!

 

以上是关于[RxJS] Filtering operator: single, race的主要内容,如果未能解决你的问题,请参考以下文章

[RxJS] Filtering operators: throttle and throttleTime

[RxJS] Filtering operators: distinct and distinctUntilChanged

[RxJS] Filtering operator: single, race

RxJS过滤数据流操作符学习 (Filtering Operators)

rxjs/operators:找不到模块

[RxJS] What RxJS operators are