I hava a list of streets and iterate over them. Inside the for loop i gonna call an endpoint for each of these streets. The endpoint gives me informations about the requested street. I wanna store each response into an Object Array and after ALL requests finished, i wanna execute the next code block. Here is my problem: I do all the calls, store all my data into the Object Array, but if i gonna use in the next code block my prefilled Object Array, the length = 0... Here my code:
export class MyComponent{
addressInfoArray: AddressInfo[] = [];
// some code ...
prepareStreetInformations(): void {
// some code ....
this.fillArray(streets, url);
this.doSomethingWithArray(this.addressInfoArray); // <--- length = 0 and doesn't waits 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);
});
}
}
So my question is: How can wait the doSomethingWithArray() method for the completely finishing of the fillArray() method and why is the doSomethingWithArray() method can't see that my Object Array is allready filled?