Subscribing to a nested observable inside another observable

125 views Asked by At

I have a behaviour subject which holds an observable of some data. i.e.

BehaviorSubject<Observable<data>>

How can I subscribe to the nested observable directly?

1

There are 1 answers

2
Mladen On BEST ANSWER

You should just pipe it to one of the flattening operators, like mergeMap:

yourSubject.pipe(mergeMap(v => v))

My example:

const t = timer(0, 500).pipe(take(5));
const s = new BehaviorSubject(t);
s.next(t);
s.pipe(mergeMap(v => v)).subscribe(console.log);
s.next(t);
s.next(t);