I've run into an issue where I'm attempting to invoke a service in Angular within a for loop and store the result in a Map.
map = new Map<string, string[]>();
this.effectivitiesList = this.trimEffectivities.split(",");
for (let effec of this.effectivitiesList) {
this.hexcodeService
.GetHexCodeLineValues(
this.majorModel,
this.wireNumber,
this.selected,
this.trimEffectivities,
effec
)
.subscribe(data => {
this.hexcodeLineList = data;
});
this.map.set(effec, this.hexcodeLineList);
}
console.log(this.map);
The issue arises because the data isn't retrieved successfully the first time the code is executed. The Map displays:
Map - >
VA305 , Array[0]
VA504 , Array[0]
However, on the second invocation of the method, the data is fetched correctly:
VA305 , Array[3]
VA504 , Array[3]
Any insights on why this might be happening?