combineLatest doesn't wait for observable to emit value

Peter Boomsma

I have 2 services which both have a observable, and a component that should get the last emitted value of both observables but the combineLast already fires when 1 observable changes, not both:

export class CreateSessionComponent {
  constructor(
    private sessionService: SessionService,
    private userService: UserService
  ) {
    combineLatest([this.userService.user$, this.sessionService.session$])
      .subscribe({
        next: (data) => console.log(data),
      });
  }

  public createUserAndSession(): void {
    this.sessionService.createSession();
    this.userService.createUser();
  }
}
export class UserService {
  private userSubject = new BehaviorSubject<any | null>(null);
  public user$ = this.userSubject.asObservable();

  public createUser(): void {
    setTimeout(() => {
      this.userSubject.next(`User ${Math.random()} `);
    }, 5000);
  }
}
export class SessionService {
  private sessionSubject = new BehaviorSubject<any | null>(null);
  public session$ = this.sessionSubject.asObservable();

  public createSession(): void {
    setTimeout(() => {
      this.sessionSubject.next(`Session ${Math.random()} `);
    }, 2500);
  }
}

When the function is called I get a value in the combineLatest() at both 2500ms and 5000ms.

https://stackblitz.com/edit/angular7-rxjs-g435xp

Harshit T

combineLatest works on the principle: When any observable emits a value, emit the last emitted value from each.

If you want to wait for both of your observables to emit the values, you should use zip instead of combineLatest, I've modified your below create-session.component.ts:

import { Component } from '@angular/core';
import { zip } from 'rxjs';
import { SessionService } from '../session.service';
import { UserService } from '../user/user.service';

@Component({
  selector: 'app-create-session',
  templateUrl: './create-session.component.html',
  styleUrls: ['./create-session.component.css'],
})
export class CreateSessionComponent {
  constructor(
    private sessionService: SessionService,
    private userService: UserService
  ) {
    zip(this.userService.user$, this.sessionService.session$)
      .subscribe({
        next: (data) => console.log(data),
      });
  }

  public createUserAndSession(): void {
    this.sessionService.createSession();
    this.userService.createUser();
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

combineLatest doesn't emit latest value

Combine subject and observable and wait for subject to emit a value then emit observable

Created observable doesn't emit anything

combineLatest doesn't catch the last emitted value

Emitter doesn't emit the value

Rx combineLatest wait for two observables to emit

Promise from observable doesn't wait to be fulfilled

Rxjs combinelatest observable value is undefined

Why not all streams are emit value in combineLatest?

Emit new value to Observable

Why `BehaviorSubject` doesn't emit the last value

RxJs operator combineLatest doesn't catch the last emitted value

Combine 2 PublishSubject's and emit with Observable.combineLatest

Angular how to wait for an http observable to emit

Zip seems not to wait for Observable to emit data

RXJS Emit first result and then wait for another observable

Emit Observable value and handle it in component

wait for value from observable

RxJS combineLatest: how to get emit after just one value changes?

RxJs: Wait on value from Observable, if it doesn't come after a specified time period do some stuff and wait again, after n tries throwError

Angular observable doesn't get initial value

Rxjs of operator doesn't emit value when using proxies

RxJS operator that doesn't emit to inner observable again until its completed

IObservable<> through CombineLatest doesn't fire

Rxjs: map method doesn't execute on combineLatest

Get the latest value of an Observable and emit it immeditely

Rxjs: Execute code and emit value on completion of Observable

Emit value of the observable depends on others observables

How to emit only one or last value of Observable?