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

325
Views
Observable.forkJoin of Angular Route Params Observable

In my Angular component ngOnInit() I want to:

  1. Read route parameter and execute a dependent HTTP call with it as argument.
  2. Execute separate HTTP call.
  3. Wait for both HTTP calls to finish.

Both HTTP calls are always executing, but when my first observable is wired up to route.params, the forkJoin(...).subscribe(...) method never runs. If I replace this.route.params with Observable.of({id: 1234}) forkJoin().subscribe() gets called properly.

// VERSION 1 forkJoin().subscribe() never gets called
var dependentObservable = this.route.params
    .switchMap(params => {
        this.myId = +params['id'];
        return this.myService.getMyInfo(this.myId);
    });

// VERSION 2 forkJoin().subscribe gets called
var dependentObservable = Observable.of({id: 123})
    .switchMap(params => {
        this.myId = +params['id'];
        return this.myService.getMyInfo(this.myId);
    });

var independentObservable = this.myService.getOtherInfo();

Observable.forkJoin([dependentObservable, independentObservable])
    .subscribe(
        results = { ... },
        error => { ... },
        () => { ... }
    );
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

My guess is route.params is never completed. That's why forkJoin does not work. You can use combineLatest instead of forkJoin

about 4 years ago · Santiago Trujillo Report

0

According to this doc forkJoin emit the last value from each observables, when all observables completed.

Observable.of({id: 123}) 

will be compleated immediately after giving {id: 123}

But

this.route.params

Will never be completed. Since route params can be changed by a user when he edits browser address bar. This changes will be never stopped.

I think you can complete observable using take(1):

 this.route.params.take(1)

Or, If you want to be responsive to changing of route params use combineLatest

Observable.combineLatest(dependentObservable, independentObservable)
about 4 years ago · Santiago Trujillo 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!