0

Let´s say I have something like the code below

this.sharedService.getvalorMensalObservable().pipe(
  switchMap(x => {
    this.valorMensal = x;
    return this.simuladorGvService.obterTaxasRecebaRapido(this.valorMensal);
    //instead of returning, I wish to make more service calls and then more than one subscribe

  })
).subscribe(response => {
  this.taxas = response;
  if (this.taxas != null) {
    this.taxasModificadas = true;
  }
});

I need a good example of how can I make multpiple service call, so instead of returning from one service and then execute a subscribe, I wish to make more calls and more subscribe actions. Actually, if there is more than one way, I would like to know each good way it´s possible...Promise, any Map Structure (ConcatMap?). Examples are really welcome here

  • That really depends on what needs to happen. Multiple calls are no problem but what needs to happen with the result of those calls? – Alexander May 04 '20 at 11:59

1 Answers1

0

There are multiple operators to map one observable to another. Each has their unique use case. It all depends how the observables are co-dependent on each other. I'd recommend you to see here to know the difference between common mapping strategies.

In regards to the code you posted, you could continue piping operators for multiple calls.

this.sharedService.getvalorMensalObservable().pipe(
  switchMap(x => {
    this.valorMensal = x;
    return this.simuladorGvService.obterTaxasRecebaRapido(this.valorMensal);
  }),
  switchMap(x => this.otherService.transformData(x)),
  switchMap(x => this.someotherService.transformData(x)),
  .
  .
).subscribe(response => {
  this.taxas = response;
  if (this.taxas != null) {
    this.taxasModificadas = true;
  }
});
ruth
  • 29,535
  • 4
  • 30
  • 57