【问题标题】:RxJS observable concat not workingRxJS observable concat 不工作
【发布时间】:2017-10-11 02:31:42
【问题描述】:

concat 通话发生了什么?我知道如果我将concat 替换为merge,则代码可以正常工作,输出为foobarquxquux。我读过关于 Hot and Cold observables 的文章,我知道如果值是在订阅之前生成的,那么热的 observables 可能会发生,但是我下面的 observables 是cold,所以我想情况并非如此。

const Rx = require('rxjs');

const observable1 = Rx.Observable.create((observer) => {
  observer.next('foo');
  observer.next('bar');
  return observer;
});
const observable2 = Rx.Observable.create((observer) => {
  observer.next('qux');
  observer.next('quux');
  return observer;
});
const result1 = observable1.concat(observable2);
result1.subscribe((x) => console.log(x));

// outputs
foo
bar

https://codepen.io/thiagoh/pen/WZyrRL

【问题讨论】:

    标签: javascript rxjs observable reactivex


    【解决方案1】:

    我相信observer1需要complete(),然后concat才能开始输出observer2。

    已修改CodePen

    【讨论】:

    • 谢谢。您是否有任何来源可以确认/解释为什么需要这样做?
    • @thiagoh 这是concat() 方法的正常行为。 Subscribe to observables in order as **previous completes**, emit values. -- learnrxjs.io/operators/combination/concat.html
    • 大理石图也是很好的视觉辅助Concat
    猜你喜欢
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2019-02-24
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多