Javascript 异步完成后如何执行另一个函数?

Javascript 异步完成后如何执行另一个函数?,javascript,jquery,angularjs,typescript,promise,Javascript,Jquery,Angularjs,Typescript,Promise,我正在制作基于angularjs和typescript的应用程序,在构造函数中调用加载函数,如 private load(): angular.IPromise<void> { const progTokInit: string = 'initial'; this.$scope.progress.start(progTokInit); return this.entityService .load(this.$scope.projectRevi

我正在制作基于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 )
     })
}
上述函数是非同步的,因此获取所有相关数据将有一个最大延迟,直到我需要等待进入下一步,即我需要调用
This.refreshCostRate(…resp.employeeRates)
仅当当前
getFinanceType()
函数完全完成其任务时

我尝试过使用异步/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
函数修改为承诺:

private getFinanceType (employeeModel:any) {
    var value = employeeModel.person.cost_center.getValue()[0]
    return this.costService.getFinanceType(value)
      .then((result) => {
        console.log('res ', result);
        employeeModel.financeType = result;
        return result;
    });
}
然后从返回的承诺链:

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
         var promiseArr = resp.employeeRates.map(_ => this.getFinanceType(_));
         promiseArr.push(resp);
         return $q.all(promiseArr);
      })
      .then(financeTypeArr) {
         var resp = financeTypeArr.pop();   
         return this.refreshCostRate(...resp.employeeRates)
          .then(() => // Another Function )
          .then(() => // Yet Anothoer Function )
     })
}
private load():angular.IPromise{
常量progTokInit:string='initial';
这是.scope.progress.start(progTokInit);
返回此.entityService
.load(此.$scope.projectRevisionUid)
.然后(resp=>{
//做foreach和发送employeeModel
//获取此人的财务类型
var promiseArr=resp.employeeRates.map(=>this.getFinanceType());
承诺推送(resp);
返回$q.all(promiseArr);
})
.然后(financeTypeArr){
var resp=financeTypeArr.pop();
返回此。刷新成本率(…分别为员工)
.然后(()=>//另一个函数)
.然后(()=>//还有另一个函数)
})
}

使用
$q.all
等待所有数据从服务器返回,然后再进入下一步。

在getFinanceType函数中,您在等待关键字之后没有返回?@KobanDavis,它不起作用我错过了返回代码,现在编辑。。但我希望这是错误的做法,等待更好的解决方案。。
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
         var promiseArr = resp.employeeRates.map(_ => this.getFinanceType(_));
         promiseArr.push(resp);
         return $q.all(promiseArr);
      })
      .then(financeTypeArr) {
         var resp = financeTypeArr.pop();   
         return this.refreshCostRate(...resp.employeeRates)
          .then(() => // Another Function )
          .then(() => // Yet Anothoer Function )
     })
}