Angular/RxJS wait for HTTP response in inner Observable

Schorsch_P

I have the following case. I have a http request which returns an observable with the following data structure

[
 {
  firstName,
  secondName,
  petId
 }
]

No I have to iterate over this array and for each person I have to do a request for the pet. Not a problem so far. The problem is that I want to have the data exactly in the order how it comes from the first response.

My idea so far:

    this.personService.getPersonData().subscribe(data => {
      data.forEach(personObj =>
        this.petService.getPetData(personObj.petId)
         .subscribe(petData => 
           this.personPetData.push({...personObj, petData: petData}))
      );
    });

But when I do it like this, the order is always different.

I would very much appreciate your answers

MoxxiManagarm

Try:

this.personService.getPersonData().pipe(
  switchMap(data =>
    forkJoin(data.map(personObj => 
      this.petService.getPetData(personObj.petId).pipe(map(pet => ({ ...personObj, petData: pet}))),
    )),
  ),
)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Rxjs Observable Interval and Angular2 HTTP: wait for response

Wait for Observable response with if condition in Angular

Assign http response to Observable

Http URLConnection wait for inner request

Call inner function but wait for response before moving on

wait for Http response in angular 2

Ionic 2 wait http response

Wait for Http response in angular 5

Wait response for http.get()

I need wait for HTTP response

Http observable response parse into array

Angular how to wait for an http observable to emit

rxjs: how to make foreach loop wait for inner observable

Wait for multiple http response using reactor

Angular2 wait on $http response

angular observable http call map response to interface

How to conditionally subscribe inside an observable, wait for response and return the complete result?

Angular 4, convert http response observable to object observable

Will JMeter wait for the HTTP response of a HTTP sample before running the next one?

How to cancel a pending request in an inner HTTP observable with RXJS?

Check if Observable (http request) returned a value and if it didnt wait for it?

Rxjs: Wait for outer observable, pass value to inner and return both resolved values in subscription

Delphi send HTTP GET request without wait for the server response

NodeJS http: Wait for another response in a request listener before returning

wait for http.request response before executing further

How to wait till the response comes from the $http request, in angularjs?

How to wait for the response of http request from Protractor side

Wait for an event to happen before sending HTTP response in NodeJS?

How to wait for HTTP Request Response + Confirm Dialogue PopUp + Angular 8