如何在其他函数之前调用异步函数

时间:2018-05-21 12:35:33

标签: javascript .net angularjs

我有删除报告的功能,这是我的代码

public deleteCustomReport(dataItem): void {
      var scheduleNames = this.getScheduleNames(dataItem.id);      

      this.confirmDelete(scheduleNames, dataItem);
    }

函数this.getScheduleNames(dataItem.id)是异步函数,我需要在this.confirmDelete(scheduleNames, dataItem)之前调用它才能使我的代码正常工作。现在,它会在this.getScheduleNames(dataItem.id)之后调用this.confirmDelete(scheduleNames, dataItem)并且var scheduleNames不会发现。

以下是this.confirmDelete(scheduleNames, dataItem)功能

的代码
private confirmDelete(scheduleNames, dataItem): void {

      var confirmDeletion = angular.copy(this.constantService.confirmDeleteSwalSettings);

      if (scheduleNames != null) {
        confirmDeletion.text = "Are you sure you want to delete " + dataItem.name + " report?";
      }
      else {
        confirmDeletion.text = "This report is in schedual. Are you sure you want to delete " + dataItem.name + " report?";
      }

      this.sweetAlert.swal(confirmDeletion, (isConfirm) => {
        if (isConfirm) {
          this.reportsService.deleteCustomReport(dataItem.id)
            .then(data => {
              this.getReports();
            });
        }
      });
    }

this.getScheduleNames(dataItem.id)

private getScheduleNames(id): string {
      var name = "";

      this.reportsService.checkIfReportIsInSchedule(id)
        .then(data => {
          name = data;
        });

      return name;
    }

0 个答案:

没有答案