I need to be able to loop an array of objects, check a certain property of the object and if it's true, make a HTTP request to get extra data for the object.
Currently, I have written it as I logically thought about it in my head:
public refreshParticipants(housingTransactionId: string): Observable<Array<Participant>> {
const url: string = `${config.apiBaseUrl}`;
this.authHttpService.get(url)
.subscribe(response => {
let participants = response.json().map(p => Participant.createFromResponse(p));
participants.forEach(participant => {
if (participant.isBusinessUser) {
this.getParticipantBranchFromHousingTransaction(participant.id, housingTransactionId)
.subscribe((branch) => {
participant.branchId = branch.id;
})
}
this.participants.push(participant);
});
return Observable.of(this.participants);
});
My problem is that the requests aren't being waited for, therefore this.participants isn't getting populated.
I'm not sure how to write it so that this.participants is populated with both participants who have needed the extra data from the HTTP request and the participants who didn't.