user5047085
user5047085

Reputation:

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

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?

Upvotes: 2

Views: 618

Answers (1)

Mark van Straten
Mark van Straten

Reputation: 9425

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>

Upvotes: 2

Related Questions