You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请问哪个RxJS操作符可在任一源Observable发射时输出值且不终止?

符合需求的RxJS操作符:merge

你需要的是RxJS的merge操作符,它的行为完全匹配你的需求:

  • 同时订阅所有传入的源Observable
  • 每当任意一个源Observable发射值时,立即将该值传递给下游Observable
  • 不会在某个源发射值后停止订阅其他源,会持续接收所有后续发射的值

对应你给出的时序示例,merge(source1, source2, source3)会按照1 -> 2 -> 3 -> 4 -> 5 -> 6的顺序发射值,和你预期的结果完全一致。

简单代码示例:

import { merge, interval } from 'rxjs';
import { map, take } from 'rxjs/operators';

// 模拟三个源Observable
const source1 = interval(2000).pipe(map(i => i*3 +1), take(2)); // 发射1,4
const source2 = interval(1000).pipe(map(i => i*3 +2), take(2)); // 发射2,5
const source3 = interval(1500).pipe(map(i => i*3 +3), take(2)); // 发射3,6

merge(source1, source2, source3).subscribe(val => console.log(val));
// 输出顺序:1,2,3,4,5,6

内容的提问来源于stack exchange,提问作者char m

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 16:45:44