【发布时间】:2019-10-04 12:56:16
【问题描述】:
我正在制作基于 angularjs 和 typescript 的应用程序,在构造函数中我正在调用类似的加载函数,
private load(): angular.IPromise<void> {
const progTokInit: string = 'initial';
this.$scope.progress.start(progTokInit);
return this.entityService
.load(this.$scope.projectRevisionUid)
.then(resp => {
//Doing foreach and sending employeeModel
// to get the financetype of the person
resp.employeeRates.forEach(employeeModel => this.getFinanceType(employeeModel));
this.refreshCostRate(...resp.employeeRates)
.then(() => // Another Function )
.then(() => // Yet Anothoer Function )
})
}
这里我需要获取一个人的财务类型,如果我发送成本中心号码,我将获得数据,所以我在getFinanceType() 函数中给出了这样的数据,
private getFinanceType (employeeModel:any) {
this.costService.getFinanceType(employeeModel.person.cost_center.getValue()[0])
.then((result) => {
console.log('res ', result);
employeeModel.financeType = result;
return result;
});
}
上面的这个函数是异步的,所以在获取所有相关数据之前会有一个最大延迟,直到那时我需要等待移动到下一步,即,只有当当前的 getFinanceType () 函数完成时,我才需要调用 this.refreshCostRate(...resp.employeeRates)它的任务完全..
我尝试过使用 async/await 之类的,
private async getFinanceType (employeeModel:any) {
await return this.costService.getFinanceType(employeeModel.person.cost_center.getValue()[0])
.then((result) => {
console.log('res ', result);
employeeModel.financeType = result;
return result;
});
}
但没有任何帮助,即使此数据未完成,执行仍在继续,因此我无法在正确的时间设置 employeeModel.financeType 的数据。
请帮助我处理这种情况,等待这个getFinanceType() 完成它的工作,然后在执行另一个函数和另一个函数之后......
【问题讨论】:
-
在您的 getFinanceType 函数中,您没有在 await 关键字之后返回?
-
@KobanDavis,它不起作用我错过了代码中的返回,现在编辑..但我希望它的方法错误,等待更好的解决方案..
标签: javascript jquery angularjs typescript promise