本文主要是介绍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(() => []);// 准备所有的Promiseconst promises = this.uriCheckedList.map((item, index) => {this.queryParams.lpProjectId = item.lpProjectIdthis.lpProjectId = item.lpProjectIdthis.lpTestpointPropertyUri = item.lpTestpointPropertyUrithis.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,};// 返回Promisereturn 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已完成,更新chartDatathis.chartData = this.finallyData;console.log("this.chartData",JSON.stringify(this.chartData))} catch (error) {// 处理中有一个Promise失败,你可以在这里处理console.error("An error occurred:", error);}}
这篇关于vue使用Promise.all可以同时执行多个异步操作,,并将这些异步操作的结果一并返回的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!