Currently, I am facing a challenge where I need to loop through an array of objects, check a specific property of each object, and if it meets certain criteria, make an HTTP request to fetch additional data for that object.
The code snippet below represents how I initially approached the problem:
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);
});
Despite my effort, the issue arises from requests not being executed in sequence, causing this.participants
to remain empty.
I am currently seeking advice on how to implement the logic in such a way that this.participants
includes both objects that required additional HTTP data and those that did not.