完成异步功能后调用一个功能

时间:2019-04-30 15:07:12

标签: angular callback observable subscribe

我看到一个接一个的用诺言调用函数,我有一个可观察的异步函数。

getHistoryData() {
    this.generatePayload()
    this.historyDataService
      .getHistoryData(this.payload)
      .subscribe(
        (data: any) => {
          const responeData: any = data.dataPoints;
          this.timestampArray = responeData.map(e => e.date.slice(5, 10) + ' ' + e.timestamp.slice(0, 5));
          console.log('temp array' + this.temperatureArray)
          this.temperatureArray = responeData.map(e => e.parameter1 / 100);
          this.dateArray = responeData.map(e => e.date);
        },
      )
  }  
other function is :

 updateChartData(){
  console.log('in update chart data')
  console.log('temp values' + this.temperatureArray)
  this.chartData[0].data =  this.temperatureArray
  this.labels = this.timestampArray
}

我想一个接一个。怎么做?

1 个答案:

答案 0 :(得分:2)

解决方案1:订阅():

的回调中调用 updateChartData()
getHistoryData() {
this.generatePayload()
this.historyDataService
  .getHistoryData(this.payload)
  .subscribe(
    (data: any) => {
      updateChartData();         
    },
  )
}

解决方案2 :使用 switchMap (),您可以访问switchMap中的数据:

getHistoryData() {
  this.generatePayload()
  this.historyDataService
    .getHistoryData(this.payload)
    .subscribe(
      (data: any) => {}
    .switchMap((data) => {
      updateChartData();
    })
  )