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

142
Visualizações
RxJS use combined emissions as they come

I have an Angular component which needs data from three separate sources, call them s1$, s2$ and s3$. These are, in fact, API calls.

I'm creating a combined value called pageData$, essentialy an object which will hold all three values for the template.

I also want the data displayed as it comes instead of waiting for all requests to complete.

I tried the mergeMap, but realized that this won't work because every nested Observable needs to emit at least once in order for the mapped result to emit. Since these three sources are in fact HTTP requests, this essentially means I'm getting a forkJoin-like behavior.

So, I tried with combineLatest, but it still behaves the same. Here's the code:

constructor(route: ActivatedRoute) {
    // Attempt with mergeMaps:
    this.pageData$ = route.params.pipe( // I get some Id from route parameters
        switchMap(params => s1$.pipe(
            mergeMap(s1 => s2$.pipe(
                mergeMap(s2 => s3$.pipe(
                    map(s3 => {
                        return {
                            data1: s1,
                            data2: s2,
                            data3: s3
                        };
                    })
                ))
            ))
        ))
    );

    // Attempt with combineLatest:
    this.pageData$ = route.params.pipe(
        switchMap(params => this.getPageData$(params['id']))
    );
}

private getPageData$(eventId: string): Observable<EventGroupPageData> {
    return combineLatest([
        s1$,
        s2$,
        s3$
    ]).pipe(
        map(([s1, s2, s3]) => {
            return {
                data1: s1,
                data2: s2,
                data3: s3
            };
        })
    );
}

In template, I'm using an async pipe to unpack the values and render the UI:

<div *ngIf="pageData$ | async as pageData"> render... </div>

Now, I'd like to be able to render each of the three element as soon as that particular piece of data comes from the API, which I can't seem to achieve.

Any ideas?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think you can use combineLatest, but you need to start each observable with start value, for example it can be a startWith operator, there is an example for you: Stackblitz

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