I have a scenario where I am iterating over a list of streets and making API calls for each street. I am storing the responses in an Object Array and want to execute the next code block only after all the requests are finished. However, I am facing an issue where the Object Array appears to be empty when trying to use it in the next code block. Here is the relevant code snippet:
export class MyComponent{
addressInfoArray: AddressInfo[] = [];
// some code ...
prepareStreetInformations(): void {
// some code ....
this.fillArray(streets, url);
this.doSomethingWithArray(this.addressInfoArray); // <--- length = 0 and doesn't wait for finishing the fillArray() method
}
}
fillArray(streets: Street[], url: string): void { // streets has a length of 150
for (const street of streets) {
this.http.get<AddressInfo>(`${url}/street.name`).subscribe(response => {
this.addressInfoArray.push(response);
});
}
}
My question is: How can I make the doSomethingWithArray() method wait for the fillArray() method to completely finish, and why does it not recognize that the Object Array is already filled?