Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!