Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
Returning a combineLatest observable from a function with an argument

I'm not really sure if I'm doing this correctly.

I have a simple function that has an argument which I want to use inside a combineLatest observable. That observable is then returned.

  fn(arg) {
    return combineLatest([
      observable...,
      observable...,
    ]).pipe(
      map(() => arg)
    );
  }

The issue here is that when the function is called many times, it creates duplicate observables.

What would be the proper solution? To make the argument an observable too? So the function is only called once but it returns the correct value when the argument changes.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Solution is not use Observables this way. Use a Subject for that kind of things:

readonly actionSubject = new Subject<unknown>();

...

ngOnInit(): void {
  this.actionSubject.pipe(
    switchMap(newValue => 
      combineLatest([
        observable...,
        observable...,
      ]).pipe(map(() => newValue)),
    )
  ).subscribe();
}


...
ngOnDestroy(): void {
  this.actionSubject.complete();
}

And in place of your function usage instead of fn:

this.actionSubject.next(someNewValue);

What happens here? SwitchMap will stop you old thread and start new one every time you have new value. That's solution.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda