Observable.prototype.concatAll does not seem to yield expected result

杀马特。学长 韩版系。学妹 提交于 2019-12-11 00:34:04

问题


With this code in mind:

const Rx = require('rxjs');

var i = 3;

const obs = Rx.Observable.interval(10)
    .map(() => i++)
    .map(function(val){
        return Rx.Observable.create(obs => {
            obs.next(val)
        });
    })
    .take(10)
    .concatAll();


obs.subscribe(function(v){
    console.log(v);
});

I would have expected the logged result to be something like:

[3,4,5,6,7,8,9,10,11,12]

That is, 10 values, starting with 3.

However, all we get is just

3

Does anybody know why that would be?


回答1:


concatMap will wait for the first observable to complete before subscribing to the next. You forgot to add the .complete() to your inner observable, effectively having your stream only emit the first value 3 and waiting indefinitely for the first stream to complete before concatting the next to it.

Note; for a simple value emission as per your question you can also use Rx.Observable.of() instead of Rx.Observable.create()

var i = 3;

const obs = Rx.Observable.interval(10)
  .map(() => i++)
  .map(val => Rx.Observable.of(val))
  .take(10)
  .concatAll();

obs.subscribe(v => console.log(v));
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.0.3/Rx.js"></script>


来源:https://stackoverflow.com/questions/41529308/observable-prototype-concatall-does-not-seem-to-yield-expected-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!