[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!
复制代码

 

posted @   Zhentiw  阅读(319)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2015-06-05 [D3] 14. Line and Area Charts with D3
2015-06-05 [D3] 13. Cleaner D3 code with selection.call()
2015-06-05 [D3] 12. Basic Transitions with D3
点击右上角即可分享
微信分享提示