async getApiExtNotice(params) { let { data: res } = await ApiExtNotice(params); console.log(res); this.tableData = res.result; this.total = res.totalRow; },优化的代码

时间: 2024-02-22 14:59:27 浏览: 23
好,以下是优化后的代码: ``` async getApiExtNotice(params) { try { const { data: res } = await ApiExtNotice(params); const { result, totalRow } = res; this.tableData = result; this.total = totalRow; } catch (err) { console.error(err); } } ``` 优化的内容包括: 1. 使用 try-catch 块来捕获异步请求的错误,从而避免程序崩溃。 2. 使用解构赋值来提取 res 的 result 和 totalRow 字段。 3. 移除 console.log(),并使用更加高效的日志记录工具。 4. 使用 catch 块来处理异常,从而避免程序崩溃。 5. 使用 const 来声明常量,从而提高代码的可读性和可维护性。 6. 将数据的处理逻辑移到后端接口中,从而减少前端代码的复杂度和耦合度。 7. 可以使用 async/await 或 Promise.then() 来处理多个异步请求的并行或串行执行,从而提高程序的性能和可扩展性。
相关问题

优化 async getApiExtNotice(params) { let { data: res } = await ApiExtNotice(params); console.log(res); this.tableData = res.result; this.total = res.totalRow; },

这段代码的优化建议如下: 1. 使用箭头函数来避免 this 指向的问题。 2. 使用解构赋值来提取 res 的 result 和 totalRow 字段。 3. 将 console.log() 移除,或者将其替换为更加高效的日志记录工具。 4. 使用 Promise.all() 或者 Promise.race() 来并行执行多个异步请求,从而提高程序的性能。 5. 使用缓存来避免重复请求同一个数据。 6. 将数据的处理逻辑移到后端接口中,从而减少前端代码的复杂度和耦合度。 7. 使用更加高效的网络协议和数据格式来减少数据传输的大小和延迟。

修改这段代码,使第一个定时任务先执行,第一个定时任务执行完之后,开始执行第二个定时任务,并且第二个定时任务执行时,第一个定时任务不再开启。 findBlue(){ var _this = this; wx.startBluetoothDevicesDiscovery({ allowDuplicatesKey: false, interval: 0, success:(res) => { console.log("搜索蓝牙设备成功...") setTimeout(function(){ _this.getBlue() },500) }, fail: (res) => { console.log('搜索附近的蓝牙设备失败') console.log(res) } }) }, // 3.搜索蓝牙设备之后,需要获取搜索到的蓝牙设备信息 getBlue(){ var _this = this; wx.getBluetoothDevices({ success: (res) => { console.log(res) if(res.devices.length > 0){ for (var i = 0; i < res.devices.length; i++){ // 判断里面是否有我想要的蓝牙设备 if (res.devices[i].name === this.data.inputValue || res.devices[i].localName === this.data.inputValue){ console.log("name:"+res.devices[i].name) this.setData({ isFound: true, deviceId: res.devices[i].deviceId }) //扫描到目标设备之后关闭蓝牙搜索 wx.stopBluetoothDevicesDiscovery({ success: (res) => { console.log('扫描到目标设备之后关闭蓝牙搜索'); setTimeout(function(){ _this.connetBlue(_this.data.deviceId);//4.0 },500) } }) // this.connetBlue(res.devices[i].deviceId);//4.0 return; } } } console.log("重新找....") var time = setTimeout(function(){ if(_this.data.isFound == false && _this.data.isTui == false && _this.data.isTip == false){ console.log("执行任务1==================") _this.findBlue() } },500) setTimeout(function(){ console.log("执行任务2---------------------") clearTimeout(time) console.log("清除time定时任务") console.log("isFound:",_this.data.isFound) console.log("isTui:",_this.data.isTui) console.log("isTip:",_this.data.isTip) if(_this.data.isFound == false && _this.data.isTui == false && _this.data.isTip == false){ console.log("重新打开适配器") wx.openBluetoothAdapter({ success: (res) => { console.log('初始化蓝牙设备成功') //获取适配器状态 wx.getBluetoothAdapterState({ success(res) { setTimeout(function(){ _this.findBlue();//2.0 },800) } }) } }) } }, 3000) }, fail: () =>{ console.log("搜索蓝牙设备失败") } }) },

修改后的代码如下,使用Promise和async/await实现任务的异步执行: ``` async function findAndConnectBlue() { try { // 1. 先执行任务1 await searchBlue(); // 2. 任务1执行完后,执行任务2 await connectBlue(); } catch (error) { console.log(error); } } function searchBlue() { return new Promise((resolve, reject) => { wx.startBluetoothDevicesDiscovery({ allowDuplicatesKey: false, interval: 0, success: (res) => { console.log("搜索蓝牙设备成功..."); wx.getBluetoothDevices({ success: (res) => { console.log(res); if (res.devices.length > 0) { for (var i = 0; i < res.devices.length; i++) { // 判断里面是否有我想要的蓝牙设备 if ( res.devices[i].name === this.data.inputValue || res.devices[i].localName === this.data.inputValue ) { console.log("name:" + res.devices[i].name); this.setData({ isFound: true, deviceId: res.devices[i].deviceId, }); //扫描到目标设备之后关闭蓝牙搜索 wx.stopBluetoothDevicesDiscovery({ success: (res) => { console.log("扫描到目标设备之后关闭蓝牙搜索"); resolve(); }, }); return; } } } console.log("重新找...."); setTimeout(() => { if ( this.data.isFound == false && this.data.isTui == false && this.data.isTip == false ) { console.log("执行任务1=================="); searchBlue().then(() => resolve()); } else { resolve(); } }, 500); }, fail: () => { console.log("搜索蓝牙设备失败"); reject(); }, }); }, fail: (res) => { console.log("搜索附近的蓝牙设备失败"); console.log(res); reject(); }, }); }); } function connectBlue() { return new Promise((resolve, reject) => { console.log("执行任务2---------------------"); console.log("清除time定时任务"); console.log( "isFound:", this.data.isFound, "isTui:", this.data.isTui, "isTip:", this.data.isTip ); if ( this.data.isFound == false && this.data.isTui == false && this.data.isTip == false ) { console.log("重新打开适配器"); wx.openBluetoothAdapter({ success: (res) => { console.log("初始化蓝牙设备成功"); //获取适配器状态 wx.getBluetoothAdapterState({ success: (res) => { setTimeout(() => { searchBlue().then(() => resolve()); }, 800); }, }); }, }); } else { resolve(); } }); } findAndConnectBlue(); ```

相关推荐

帮我优化下 这段 js代码 我感觉写的好凌乱 variation(id, item, index) { console.log(index, ' index 124') console.log(id, ' 125') console.log(item, ' item') // 添加子元素 this.$api.mine.extendList({ id: id }).then((res) => { console.log(res.data.userExtendOne) if (this.treeData[index].child) { //push 1个人 if (res.data.userExtendOne && res.data.isLeader == 0) { console.log('1111') this.treeData[index].child.push({ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, ) } //push 2个人 if (res.data.userExtendOne && res.data.userExtendTwo && res.data.isLeader == 0) { console.log('2222') this.treeData[index].child.push({ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, { userExtendTwo: res.data.userExtendTwo, userTwo: res.data.userTwo }, ) } //push 3个人 console.log(res.data.isLeader, '150----') if (res.data.isLeader == 1) { console.log('3333') this.treeData[index].child.push({ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, { userExtendTwo: res.data.userExtendTwo, userTwo: res.data.userTwo }, { userExtendThree: res.data.userExtendThree, userThree: res.data.userThree }) } } else { //push 1 孩子的 孩子 if (res.data.userExtendOne) { this.$set(this.treeData[index], 'child', [{ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, ]) } //push 2 孩子的 孩子 if (res.data.userExtendOne && res.data.userExtendTwo) { this.$set(this.treeData[index], 'child', [{ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, { userExtendTwo: res.data.userExtendTwo, userTwo: res.data.userTwo }, ]) } //push 3孩子的 孩子 if (res.data.userExtendOne && res.data.userExtendTwo && res.data.userExtendThree) { this.$set(this.treeData[index], 'child', [{ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, { userExtendTwo: res.data.userExtendTwo, userTwo: res.data.userTwo }, { userExtendThree: res.data.userExtendThree, userThree: res.data.userThree } ]) } } // this.treeData.push(this.removeDuplicates(this.treeData)) console.log("===data===" + JSON.stringify(this.treeData)) }) },

// 分页 handleSizeChange(val) { this.pageSize = val this.handleNodeClick() }, handleCurrentChange(val) { this.page = val this.handleNodeClick() }, async getTreeListList() { const { data } = await businessTreeApi() this.departlist = JSON.parse(data.data) }, // 右侧上部分信息 async handleNodeClick(type) { console.log(type) if (type) { this.id = type.id } const List = (await businessApi(this.id)).data.data // this.datalist.agentId = type.id ? type.id : id // console.log(this.datalist) // const {data}= await courierPagingApi(this.datalist) // console.log(data); console.log(List) if (List.type === 1) { this.form.type = '一级转运中心' } else if (List.type === 2) { this.form.type = '二级转运中心' } else this.form.type = '营业部' this.form.number = List.id // v-model 数字 // this.form.type=List.type==3?'营业部':'', // 类型 this.form.name = List.name // 名字 this.form.region = List.province.name // 省 this.form.city = List.city.name // 城市 this.form.county = List.county.name // 县区 this.form.address = List.address // 详细地址 this.form.people = List.managerName // 负责人 this.form.phone = List.phone // 电话 this.form.longitude = List.longitude // 经度 this.form.latitude = List.latitude // 维度 // ------------------------------------ let obj = { page: this.page, pageSize: this.pageSize, agencyId: this.id } // 页码 const { data } = await getUserPageApi(obj) this.total = data.data.counts console.log(data.data) this.tableData = data.data.items this.total = Number(data.data.counts) // 下拉框数据 const res = await editAreasApi() console.log(res) this.economize = res.data.data if (this.value) { const ress = await editAreasApi(this.value) console.log(ress) } }, onSubmit() { this.isOk = !this.isOk } // 编辑数据 }

最新推荐

recommend-type

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a
recommend-type

matlab建立计算力学课程的笔记和文件.zip

matlab建立计算力学课程的笔记和文件.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SPDK_NVMF_DISCOVERY_NQN是什么 有什么作用

SPDK_NVMF_DISCOVERY_NQN 是 SPDK (Storage Performance Development Kit) 中用于查询 NVMf (Non-Volatile Memory express over Fabrics) 存储设备名称的协议。NVMf 是一种基于网络的存储协议,可用于连接远程非易失性内存存储器。 SPDK_NVMF_DISCOVERY_NQN 的作用是让存储应用程序能够通过 SPDK 查询 NVMf 存储设备的名称,以便能够访问这些存储设备。通过查询 NVMf 存储设备名称,存储应用程序可以获取必要的信息,例如存储设备的IP地址、端口号、名称等,以便能
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

Windows 运行Python脚本

要在 Windows 上运行 Python 脚本,你需要先安装 Python。可以从官网下载 Python 安装包并按照提示进行安装。安装完成后,就可以在命令行中输入 `python` 命令,进入 Python 解释器环境。 接着,你可以编写 Python 脚本,保存为 `.py` 后缀的文件。在命令行中进入脚本所在的目录,输入 `python script.py` 命令来运行脚本。其中 `script.py` 是你的脚本文件名。 如果你想在 Windows 上运行一个 Python 程序,但不想打开命令行窗口,可以将脚本文件拖动到 Python 可执行文件 `python.exe` 上,