Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sending two http request one after another

I have 2 api calls which i want to send one after the other.
I need a response of the 1st one to send the 2nd api request, but i also need the data from first call. Looks like switchMap is the way to go, atm it looks like this:

this.apiCall1().pipe(switchMap(res1 => this.apiCall2(res.data))).subscribe(res => ...)

but i need to assign the res1.differentData to a variable which seems impossible with switchMap. Or maybe im just missing some syntax.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I suggest you to just reemit the value coming from apiCall1() and use forkJoin to get both values at the end:

this.apiCall1().pipe(
  switchMap(res => forkJoin(
    [of(res), this.apiCall2(res)]
  ))).subscribe(console.log)

this way you receive an array containing two elements (the results of both calls) at the end.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading