Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

324
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda