Search code examples
rxjsrxjs-observablesrxjs-pipeable-operators

Altering combineLatest to emit the result without awaiting other observables


I have this rxjs code where cobineLatest is used

cobineLatest(
  this.controls.toArray().map(c => c.changeEvent.asObservable())
).subscribe(x => {
  console.log(x);
});

The thing is that there won't be any result in subscription until all observables emits, I wonder how would you change that behavior so it will start emitting even if one single observable emits?


Solution

  • I suggest you to just pipe the single observables to start with null. This way you ensure that each observable has emitted at least one value:

    cobineLatest(
      this.controls.toArray().map(c => c.changeEvent.asObservable().pipe(startWith(null)))
    ).subscribe(x => {
      console.log(x);
    });