hridayesh
hridayesh

Reputation: 1143

Why all subscribers are not called in RxJS

This is my code

let x = Rx.Observable.create(obs => window.obs = obs);
y = x.map(x=>x+2);
x.subscribe(v=>console.log("x got ",v));
y.subscribe(v=>console.log("y got ",v));
obs.next(1);

Output is

y got 3

I was expecting output to be

x got 1
y got 3

What am I missing here? Thanks

Upvotes: 0

Views: 51

Answers (1)

Mark van Straten
Mark van Straten

Reputation: 9425

You are overwriting the window.obs upon the second subscription.

If your usecase requires you to procedurally invoke emissions then you can use a Subject instead of creating an observable yourself:

const subj = new Rx.Subject();
const sub1 = subj.subscribe(x => console.log('x got: ' + x));
const sub2 = subj.map(v => v * 2).subscribe(y => console.log('y got: ' + y));
subj.next(2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.1.0/Rx.js"></script>

Upvotes: 2

Related Questions