码迷,mamicode.com
首页 > Web开发 > 详细

[RxJS] Filtering operator: single, race

时间:2016-06-05 23:10:32      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5562094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!