'In angular code, foreach loop skips to the other function before finishing the loop iteration
private processArray(evts: Event[]): Promise<void> {
var auditsvc = this.auditSvc;
var t = this;
if(!evts || evts.length ==0 ) {
return;
}
let objArr: any[] = [];
evts.forEach( function (inpEvt) {
auditsvc.getAuditDetails(inpEvt.id).subscribe((data) => {
for(let i=0; i<data.length; i++){
let outObj = {};
outObj['Driver'] = inpEvt.driver.name;
outObj['Executive'] = inpEvt.executive.exec.name;
outObj['Point of Departure'] = inpEvt.pod;
outObj['Date of Departure'] = UtilService.dateToString(inpEvt.date);
outObj['Arrival Time'] = UtilService.timeToString(inpEvt.arrivalTime);
outObj['Departure Time'] = UtilService.timeToString(inpEvt.departureTime);
outObj['Action Timestamp'] = data[i].actionedOnTimestamp;
outObj['Modified By'] = data[i].modifiedBy;
outObj['Comment'] = data[i].comment;
objArr.push(outObj);
}
}, (err) => {
console.log(err);
}, () => {
});
});
if(objArr.length == 0) {
UtilService.openError(t.modalSvc,'No logs found.');
return;
}else{
t.exportAsXLSX(objArr);
}
}
the for each loop is not executed but the next function is executed first. I want the loop to iterate first and then go to the other function as the loop returns some data required by the other function.
Since the loop is not iterated, the objArr length is 0, it always gives 'No logs found' error.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
