I am struggling with this code and it's driving me crazy.
addUpSpecificDaysOfWeek(daysInMonth: any, callbackFunction: any){
var data = [];
var that = this;
daysMonth.forEach(function(day){
that.statsService.fetchData(that.userid, day).subscribe(async (res: any) => {
data = JSON.parse(JSON.stringify(res));
console.log(that.data);
that.data = that.data.map( function(value, index) {
return value + data[index];
});
});
});
callbackFunction("this should be at the end");
}
In this piece of code, I am fetching arrays from a server and adding them together into 'that.data' element by element. The process is working as expected, but I intend to calculate an average of the final result. Currently, I'm just using the callback function to display a message and check if it reaches the end, however, "this should be at the end" is being displayed before the summation loop begins.
myCustomCallback(argument: any){
console.log(argument);
}
This is where the main method is called:
this.addUpSpecificDaysOfWeek(daysInMonth, this.myCustomCallback);