0
this.pokojeService.pobierzPokoje().subscribe(pokoje => {
  this.pokoje = pokoje;
});
this.pokojeService.pobierzTypyPokoi().subscribe(pokojeTypy => {
  this.pokojeTypy = pokojeTypy;
});

those are my 2 observables. I want to iterate on pokoje and pokojetypy, but what if one of them return error, not data? I want to prevent that and check if both of them returned data successfully

shaunhusain
  • 19,630
  • 4
  • 38
  • 51
Paweł Meller
  • 23
  • 1
  • 8

2 Answers2

0

Use the forkJoin method :

Observable.forkJoin(this.pokojeService.pobierzPokoje(), this.pokojeService.pobierzTypyPokoi()).subscribe(response => {
      if(response && response.length === 2){
        let pobierzPokojeResult = response[0];
        let pobierzTypyPokoiResult = response[1];
        // PUT YOUR CODE HERE
      }
    });
Nicolas Law-Dune
  • 1,631
  • 2
  • 13
  • 30
-1
const obs1$ =  this.pokojeService.pobierzPokoje()
obs1$.catch((e: any) => Observable.throw(this.errorHandler(e)))

const obs2$ =  this.pokojeService.pobierzTypyPokoi()
obs2$.catch((e: any) => Observable.throw(this.errorHandler(e)))

Observable.zip(obs1$,obs2$).subscribe(res=>{
  if(res[0] && res[1]){
    do Something...
  }
})

you can filter response instead of catching errors, you can transform data if you want. use obs1$.map(data=>....) make sure to return false if no data or w/e you want and this should answer your question