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

245
Views
Fusionando http observables usando forkjoin

Estoy tratando de evitar observables anidados usando forkjoin . La versión actual (anidada) se ve así:

 this.http.get('https://testdb1.firebaseio.com/.json').map(res => res.json()).subscribe(data_changes => { this.http.get('https://testdb2.firebaseio.com/.json').map(res => res.json()).subscribe(data_all => { /* Do this once resolved */ this.platform.ready().then(() => { this.storage.set('data_changes', data_changes); this.storage.set('data_all', data_all); document.getElementById("chart").innerHTML = ""; this.createChart(); }); }); }, err => { this.platform.ready().then(() => { console.log("server error 2"); document.getElementById("chart").innerHTML = ""; this.createChart(); }); }); }

Puedo reescribir la primera parte como:

 Observable.forkJoin( this.http.get('https://testdb1.firebaseio.com/.json').map((res: Response) => res.json()), this.http.get('https://testdb2.firebaseio.com/.json').map((res: Response) => res.json()) )

Pero no estoy seguro de cómo agregaría el método .subscribe para acceder tanto a data_changes como a data_all .

Mirando otro ejemplo, sé que debería verse como .subscribe(res => this.combined = {friends:res[0].friends, customer:res[1]}); , pero no estoy seguro de cómo adaptar esto a mi ejemplo.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Intente usar combineLatest en lugar de forkJoin :

Con combineLatest :

 const combined = Observable.combineLatest( this.http.get('https://testdb1.firebaseio.com/.json').map((res: Response) => res.json()), this.http.get('https://testdb2.firebaseio.com/.json').map((res: Response) => res.json()) ) combined.subscribe(latestValues => { const [ data_changes , data_all ] = latestValues; console.log( "data_changes" , data_changes); console.log( "data_all" , data_all); });

También puede manejarlo con forkJoin, pero forkJoin devolverá datos cuando todas las llamadas hayan terminado y devolverá result, pero en combineLatest Cuando cualquier observable emita un valor, emita el último valor de cada uno.

Con forkJoin :

 const combined = Observable.forkJoin( this.http.get('https://testdb1.firebaseio.com/.json').map((res: Response) => res.json()), this.http.get('https://testdb2.firebaseio.com/.json').map((res: Response) => res.json()) ) combined.subscribe(latestValues => { const [ data_changes , data_all ] = latestValues; console.log( "data_changes" , data_changes); console.log( "data_all" , data_all); });

Llame a ambos y verifique el registro de la consola, se hará una idea.

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!