Javascript 角度:在运行下一步之前,确保服务已完成

Javascript 角度:在运行下一步之前,确保服务已完成,javascript,angular,typescript,angular8,Javascript,Angular,Typescript,Angular8,我们目前正在使用Angular。 组件正在从API接收数据。在获得API数据后,它将通过数据服务转换和定制数据、连接姓氏、舍入美元金额、进行计算等。 最后一步在解析所有数据后,尝试在下拉列表中填充Sales year this.webStoreSearchHttpService.GetAllCustomerSalesData(this.customerId).subscribe((response) => { this.customerList= customerDataServic

我们目前正在使用Angular。 组件正在从API接收数据。在获得API数据后,它将通过数据服务转换和定制数据、连接姓氏、舍入美元金额、进行计算等。 最后一步在解析所有数据后,尝试在下拉列表中填充Sales year

this.webStoreSearchHttpService.GetAllCustomerSalesData(this.customerId).subscribe((response) => {

  this.customerList= customerDataService.createCustomerList(response);
  this.productList = customerDataService.createProductAnalysis(response);
  this.salesList= customerDataService.createSalesList(response);
  this.salesYearList= customerDataService.createYearList(response);

  this.salesYearItemCurrent = _.cloneDeep(this.salesYearList[0]);   <--- this goes into a Mat Select Dropdown
this.webStoreSearchHttpService.GetAllCustomerSalesData(this.customerId).subscribe((响应)=>{
this.customerList=customerDataService.createCustomerList(响应);
this.productList=customerDataService.createProductAnalysis(响应);
this.salesList=customerDataService.createSalesList(响应);
this.salesYearList=customerDataService.createYearList(响应);

this.salesYearItemCurrent=uu.cloneDeep(this.salesYearList[0]);更新

您添加了sencee
方法返回类数组,而不是承诺或可观察。这意味着您无法从外部等待异步调用完成。因此您必须更改
customerDataService
方法的返回值。我假设在该方法中有一些异步tuff已经完成了,因为您说
我要做的是确保所有4个数据服务都是完整的

旧版本

要回答您的问题,您必须知道
customerDataService
方法返回类型是什么。该方法是否返回
Promise
Observable
?取决于此,您可以使用
Promise.all
forkJoin
运算符等待所有方法完成,然后执行选择的填充。这这是一个使用可观测数据的示例:

this.webStoreSearchHttpService.GetAllCustomerSalesData(this.customerId).subscribe(response => {
    forkJoin([
        customerDataService.createCustomerList(response),
        customerDataService.createProductAnalysis(response),
        customerDataService.createSalesList(response),
        customerDataService.createYearList(response)
    ]).subscribe(([customerList, productList, salesList, salesYearList]) => {
        this.customerList = customerList;
        this.productList = productList;
        this.salesList = salesList;
        this.salesYearList = salesYearList;
        this.salesYearItemCurrent = _.cloneDeep(this.salesYearList[0]);
    });
});
或者最好避免内部订阅,并且只有一个订阅:

this.webStoreSearchHttpService.GetAllCustomerSalesData(this.customerId).pipe(
    flatMap(response => 
        forkJoin([
            customerDataService.createCustomerList(response),
            customerDataService.createProductAnalysis(response),
            customerDataService.createSalesList(response),
            customerDataService.createYearList(response)
        ])
    )
).subscribe(([customerList, productList, salesList, salesYearList]) => {
    this.customerList = customerList;
    this.productList = productList;
    this.salesList = salesList;
    this.salesYearList = salesYearList;
    this.salesYearItemCurrent = _.cloneDeep(this.salesYearList[0]);
});

这些方法返回的是类数组,不是承诺或可观察的。但是,您希望如何等待该方法完成?我假设
customerDataService
方法内部确实发生了异步的情况,因为您说过
数据服务尚未完成解析/创建。
。您好,我不允许更改数据类型从四种服务方法返回,您能为您的新更新提供一个快速代码示例吗?thanks@Artportraitdesign1如果无法更改返回值,则无法解决问题。如果发生异步事件,则必须返回承诺/可观察值,以等待其完成。是否可以显示HTML?值是否也会在稍后显示?是否在Mat下拉列表中显示延迟?