首页 > 解决方案 > 在打字稿角度5中同步

问题描述

我有以下代码。

public async getOrderInforAsync(customerOrderId) {
	return new Promise((resolve, reject) => {
          this.orderEntryService.getCommissionIncentives(customerOrderId)
        .subscribe(
          response => {
            Object.assign(this.order, response);
            this.order.bookingDate = this.order.bookingDate ? new Date(this.order.bookingDate) : null;
            this.order.estBillDate = this.order.estBillDate ? new Date(this.order.estBillDate) : null;
            this.order.orderSplit.forEach(element => {
              element.rcNumberFullName = `${this.order.customerOrderRCNumber}${element.rcNumberSuffix}`;
            });
            this.initialQuantityAllocated();
            this.orderSummary.allocatedEstCommissionPercent = this.calculateTotalOrderPercent();
            this.orderSummary.allocatedEstCommissionAmount = this.calculateTotalOrderAmount();
            this.highlight = this.checkOrderSummary(this.orderSummary.allocatedEstCommissionPercent, this.orderSummary.allocatedEstCommissionAmount);
            this.calculateAllocatedActualPercent();
            this.calculateAllocatedActualAmount();
            this.onChangeEstSalesPrice();
            resolve();
          },
          error => {
            reject();
          }
        );
    });
}

有时在 this.calculateAllocatedActualPercent() 和 this.calculateAllocatedActualAmount() 完成之前调用 resolve()。那么如何使这段代码同步运行,这意味着这段代码上的所有函数都在 resolve() 调用之前完成了呢?

标签: javascriptangulartypescript

解决方案


尝试这个 :

public async getOrderInforAsync(customerOrderId) {
    return new Promise((resolve, reject) => {
          this.orderEntryService.getCommissionIncentives(customerOrderId)
        .subscribe(
          response => {
            Object.assign(this.order, response);
            this.order.bookingDate = this.order.bookingDate ? new Date(this.order.bookingDate) : null;
            this.order.estBillDate = this.order.estBillDate ? new Date(this.order.estBillDate) : null;
            this.order.orderSplit.forEach(element => {
              element.rcNumberFullName = `${this.order.customerOrderRCNumber}${element.rcNumberSuffix}`;
            });
            this.initialQuantityAllocated();
            this.orderSummary.allocatedEstCommissionPercent = this.calculateTotalOrderPercent();
            this.orderSummary.allocatedEstCommissionAmount = this.calculateTotalOrderAmount();
            this.highlight = this.checkOrderSummary(this.orderSummary.allocatedEstCommissionPercent, this.orderSummary.allocatedEstCommissionAmount);
            await this.calculateAllocatedActualPercent();
            await this.calculateAllocatedActualAmount();
            this.onChangeEstSalesPrice();
            resolve();
          },
          error => {
            reject();
          }
        );
    });
}

async calculateAllocatedActualPercent(){
  return new Promise(resolve,reject){
    // call api 
    if (data)
      resolve(data);
    else 
      reject()
  }
}

async calculateAllocatedActualAmount(){
  return new Promise(resolve,reject){
    // call api 
    if (data)
      resolve(data);
    else 
      reject()
  }
}

推荐阅读