2
votes

I have one subject and observable, I want to combine them and emit the observable value and do some stuff with it after subject got new value. This code below works good but I don't want to have subscribe in subscribe

this.subject.subscribe(() => {
    this.observable.subscribe(b => {
      this.someStuffToDo(b);
    });

So the workflow is: next is invoked on subject -> observable is emitting value -> do some stuff with this value. I already tried combineLatest and zip, but it stops on the first emit of subject, or the order is wrong.

1
combineLatest sounds like what you need. It doesn't stop after the first emission but emits when all source Observables emit at least once then on every emission from any of the source Observables.martin
@martin combineLatest is not working in this case because it's waiting for observer to complete, subject is not emitting complete by defaultgmexo
combineLatest doesn’t wait for the Observable to finish. That´s what forkJoin does.martin

1 Answers

3
votes

If you are using RxJS 6, you may consider using pipeable operators such as switchMap. The switchMap will allows you to map the observable values into the inner observable, removing the need to nest subscriptions.

this.subject
  .pipe(
    switchMap(() => this.observable),
  ).subscribe((b) => {
    this.someStuffToDo(b);
  });

And calling next() on subject will update its values.

this.subject.next(value);