I'm desperated with this code.
getSumOfSpecificDayWeek(daysMonth: any, callback: any){
var data = [];
var that = this;
daysMonth.forEach(function(day){
that.statsService.getData(that.userid, day).subscribe(async (res: any) => {
data = JSON.parse(JSON.stringify(res));
console.log(that.data);
that.data = that.data.map( function(v, i) {
return v + data[i];
});
});
});
callback("this should be at the end");
}
Here what I'm doing is getting arrays from a server and summing it up into that.data per each component of it, this works fine but at the end I want to average the result, at this very moment I'm just calling to callback to show a message to checking whether it occurs finally, but no, "this should be at the end" is displayed before the loop starts summing.
mycallback(arg: any){
console.log(arg);
}
This is the main call to the method
this.getSumOfSpecificDayWeek(daysMonth, this.mycallback);