After calling the fetchCounties() method within the subscribe function, the value assigned to the class variable counties does not persist. When I log the temporary variable data, it shows a valid list of counties, as well as this.counties inside the subscribe block. However, upon trying to access this information outside of the subscribe block, it suddenly becomes undefined. This behavior is confusing to me as someone transitioning from Java to Angular/Typescript...
public counties: ICounty[] = [];
public getCounties(): ICounty[] {
this.fetchCounties().subscribe(data =>{
console.log("data");
console.log(data);//logs correctly
this.counties = data;
console.log("counties inside subscribe " );
console.log(this.counties);//logs correctly
});
console.log("counties outside of subscribe " );
console.log(this.counties);//logs incorrectly (empty) >:0
return this.removeInvalidCounties(this.counties); //passes empty counties list...
}