vue使用Promise.all可以同时执行多个异步操作,,并将这些异步操作的结果一并返回

vue使用Promise.all可以同时执行多个异步操作,,并将这些异步操作的结果一并返回

 async queryData() {
      if (this.uriCheckedList.length === 0){
        this.chartData = {}
      }
      this.finallyData.yData = []
      this.finallyData.xData = []
      this.finallyData.lengList = []
      this.finallyData.yData = this.uriCheckedList.map(() => []);
        // 准备所有的Promise
      const promises = this.uriCheckedList.map((item, index) => {
        this.queryParams.lpProjectId = item.lpProjectId
        this.lpProjectId = item.lpProjectId
        this.lpTestpointPropertyUri = item.lpTestpointPropertyUri
        this.queryParams.lpVariableUri = item.lpTestpointPropertyUri
        // 打印一下结果,确保日期时间值被正确处理
        console.log("this.queryParams.projectRealEndDate",this.queryParams.projectRealEndDate);
        // 使用 moment.js 对日期时间进行处理
        let hour = this.timeDiffInHours()
        this.queryParams.intervalSize = this.intervalSize * hour + "s"
        this.queryParams.sliding =  this.sliding * hour + "s"
        // 更新查询参数 (这可能需要进一步根据具体逻辑调整)
        let queryParams = {
          ...this.queryParams,
        };

        // 返回Promise
        return getList(queryParams).then(response => {
          if (response.data.length !== 0) {
            for (let j = 0; j < response.data.length; j++) {
              this.finallyData.xData.push(JSON.parse(JSON.stringify(response.data[j].lpTimeTicks)));
              this.finallyData.lengList.push("解码值" + "[" + JSON.parse(JSON.stringify(response.data[j].lpVariableUri)) + "]");
              this.finallyData.lengList = [...new Set(this.finallyData.lengList)]
              console.log("this.finallyData.lengList",index,"值:",JSON.stringify(this.finallyData.lengList))
              this.finallyData.yData[index] = this.finallyData.yData[index].concat(JSON.parse(JSON.stringify(response.data[j].lpValue)));
            }
          }
        });
      });

      // 使用Promise.all处理所有请求
      try {
        await Promise.all(promises);
        // this.finallyData.lengList = [...this.finallyData.lengList]
        // 所有Promises已完成,更新chartData
        this.chartData = this.finallyData;
        console.log("this.chartData",JSON.stringify(this.chartData))
      } catch (error) {
        // 处理中有一个Promise失败,你可以在这里处理
        console.error("An error occurred:", error);
      }
    }

你可能感兴趣的:(异步,vue,Promise.all,vue.js)