Home > front end >  RxJS - How to 'pipe' an Observable's result into another subject in one stream
RxJS - How to 'pipe' an Observable's result into another subject in one stream

Time:02-10

In RxJS I sometimes come across an occasion when I have one Observable emitting, and I would like to 'trigger' another Observable's emission.

For example:

subject1$.subscribe(result => {/**/})

//...later 

someObservable$.subscribe(result => {
  subject1$.next(result);
});

I'd rather avoid two streams, but I can't work out is there's a pipable operator I can use to 'pipe' any omissions from someObservable$ into subject1$.

CodePudding user response:

A Subject is both an Observable and an Observer.

The tap operator accepts and Observer as its parameter.

The subscribe function also accepts and Observer as its parameter.

Putting all these things together, you may look at something like this

subject1$.subscribe(result => {/**/})

//...later 

someObservable$.tap(subject1$).subscribe();

// or

someObservable$.subscribe(subject1$);
  • Related