I am currently working on a lengthy function that involves retrieving data from multiple levels of a database.
getResult() {
this.appService
.getCountries()
.subscribe(
(countries: Country[]) => {
this.countries = countries;
},
() => {},
() => {
for (let country of this.countries) {
let cities: City[] = [];
this.appService
.getCities(country.id.toString())
.subscribe(
(theCities: Cities[]) => {
cities = theCities;
},
() => {},
() => {
for (let city of cities) {
if (city.population>1000) {
console.log(city);
}
}
}
);
}
}
);
this.result = true;
}
Once all operations are completed, I expect the statement
this.result = true;
to be executed. However, it seems to be triggered before the previous code finishes execution.
Even when placed at the end of the onCompleted
function, the
this.result = true;
is invoked before the loop is fully completed.