Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda