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

210
Visualizações
Avoiding nested subscribes with combine latest when one observable depends on the result of another

I have three observables I would like to combine into a single stream. However the third observable requires a property from the first observable -- is it possible to combine all three to avoid nested subscribes (for the sake of clarity)?

Observable.combineLatest(this.fooService.model, this.barService.model)
  .subscribe(result => {
    //do work

    this.bazService.anotherObservable(result[0].someProperty)
      .subscribe(anotherResult => {
        //do more work
      });
  });
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can use switchMap and map to do what you want:

Observable
  .combineLatest(this.fooService.model, this.barService.model)
  .switchMap(([foo, bar]) => {
    return bazService
      .anotherObservable(foo.someProperty)
      .map((baz) => [foo, bar, baz]);
  })
  .subscribe(([foo, bar, baz]) => console.log(foo, bar, baz));

Whenever the combined foo and bar observables emit a value, the baz service will be called and its result will be futher combined.

Note that switchMap will abandon pending baz calls if the foo or bar observables re-emit. If you don't want that behaviour and always want the baz result to be emitted, use concatMap instead of switchMap.

Another option is to use mergeMap instead of concatMap - the latter will guarantee that the order of the baz calls will be preserved, the former won't.

over 4 years ago · Santiago Trujillo 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