Chrillewoodz
Chrillewoodz

Reputation: 28318

Zip isn't emitting values after the first values from both observables have been emitted

I'm trying to combine two observables, one of em being route params and the other one a custom one.

I'm using zip because forkJoin doesn't seem to work. But the zip is acting strange, cuz when I emit my custom one I don't get that value. I only get the first null value which has been given to my BehaviorSubject as a default emit value.

I read this

"The zip operator will subscribe to all inner observables, waiting for each to emit a value. Once this occurs, all values with the corresponding index will be emitted. This will continue until at least one inner observable completes."

Does that mean that it won't respond until both of the observables emit a value? Because the route params observable will only emit once, but the dataEmitter will continue to emit values. What would be the correct operator to use in this case?

Here's some of my code:

Emitter in my service:

private dataEmitter: BehaviorSubject<any> = new BehaviorSubject<any>(null);

Component subscription, only get null from the dataEmitter, never this.calculatedData:

Observable.zip(this.route.params, this.dataCalculator.dataEmitter$)
  .subscribe(data => console.log(data));

This is called when all of the calculations have been done:

this.dataEmitter.next(this.calculatedData);

Upvotes: 5

Views: 2072

Answers (2)

Chrillewoodz
Chrillewoodz

Reputation: 28318

As pointed out by @cartant switching to combineLatest works as expected:

Observable.combineLatest(this.route.params, this.dataCalculator.dataEmitter$)
  .subscribe(data => console.log(data));

Upvotes: 2

Robin Dijkhof
Robin Dijkhof

Reputation: 19278

Your zip works just fine. null is a value. You could use dataEmitter.filter(val => !isNullOrUndefined(val)) to filter the null and undefined values.

Upvotes: 1

Related Questions