Sergino
Sergino

Reputation: 10818

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?

Upvotes: 0

Views: 42

Answers (2)

MoxxiManagarm
MoxxiManagarm

Reputation: 9124

If your are interested in the emitted value only (not the array), then merge might be your friend.

merge(
  this.controls.toArray().map(c => c.changeEvent.asObservable())
).subscribe(x => {
  console.log(x); // x is not an array
});

Upvotes: 0

Fabian Strathaus
Fabian Strathaus

Reputation: 3570

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);
});

Upvotes: 2

Related Questions