[英]Wait for For loop to finish - Angular
I have this code that formats the parameters and submits to api.我有这段代码可以格式化参数并提交给 api。
for (let a = 0; a < perfCriteria.length; a++) {
const element = perfCriteria[a];
let newObject = Object.assign({}, paramObject)
newObject.performanceCriteria = element.performanceCriteriaId
for (let b = 0; b < element.performanceIndicators.length; b++) {
const element2 = element.performanceIndicators[b];
let newObject2 = Object.assign({}, newObject)
newObject2.performanceIndicator = element2.performanceIndicatorId
newObject2.numberTarget = element2.divisionTarget ? element2.divisionTarget : ""
newObject2.targetDetails = element2.targetDetails
for (let c = 0; c < element2.targetDetails.length; c++) {
const element = element2.targetDetails[c];
element2.targetDetails[c].individualAccountable = element.individualAccountable.map(function(row){
return row._id
})
}
divTargetArray.push(newObject2)
}
}
this.DivisionTarget.create(divTargetArray).subscribe(result =>{
console.log('result', result)
if(result.success){
this.appComponent.showLoadingIndicator = false;
this.router.navigate(['/index/app/performanceManagement/divisionTargets']);
this.toastService.openSnackBar(result.message, "success");
}else{
this.appComponent.showLoadingIndicator = false;
this.toastService.openSnackBar(result.message, "danger");
}
}, error => {
this.appComponent.showLoadingIndicator = false;
this.toastService.openSnackBar(error, "danger");
});
But sometimes, it will not wait for the For loop to finish.但有时,它不会等待 For 循环完成。 Is there a way that I can wait for the For loop to finish before proceeding to submit it to API?有没有一种方法可以等待 For 循环完成,然后再将其提交给 API?
I can't see any asynchronous operations in your code so I think it's executed line by line.我在您的代码中看不到任何异步操作,所以我认为它是逐行执行的。 It seems like there is no possibility that subscription happens before loop has ended.似乎不可能在循环结束之前发生订阅。 Maybe issue is somewhere else?也许问题在其他地方?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.