網站首頁 Vue 正文
Vue的同步和異步調用順序
Vue中的方法調用順序是依次進行的,方法體內部也是依次執行的,但是,兩個方法體的執行順序并不能嚴格控制。
以下方法中都帶有promise函數或異步調用。
?? ?initUserData() { ?? ? ?this.getPsCountryList() // 1 獲取國家列表stateOptions,方法內同步 ?? ? ?this.getTimeZone() // 2 獲取時區timezones,方法內同步 ?? ? ?this.getUserInfo() // 3 獲取用戶信息 ?? ?}
在實際運行中,三個方法的執行順序是1-2-3,但是方法3始終不能獲取到stateOptions和timezones
背后的調用順序是1-2-3,但是,方法的執行時間并沒有嚴格控制。
如果想要做到方法調用和執行是同步的,可以使用async和await修飾符。
例如
?? ?async initUserData() { ?? ? ?await this.getPsCountryList() // 1 獲取國家列表stateOptions,方法內同步 ?? ? ?await this.getTimeZone() // 2 獲取時區timezones,方法內同步 ?? ? ?await this.getUserInfo() // 3 獲取用戶信息 ?? ?}
Vue兩個異步方法順序執行
需求:兩個異步函數按順序執行,首先獲取第一個異步函數的返回的值,接著在第二個異步函數里面調用
方法:先在第一個異步函數里返回一個promise,接著用async和await調用它
第一個異步方法
getAllNotice() { ?? ??? ??? ??? ?let data = { ?? ??? ??? ??? ??? ?"searchParams": [{ ?? ??? ??? ??? ??? ??? ?"fieldName": "equipmentId", ?? ??? ??? ??? ??? ??? ?"operate": "eq", ?? ??? ??? ??? ??? ??? ?"value": "000000" ?? ??? ??? ??? ??? ?}], ?? ??? ??? ??? ??? ?"size": -1 ?? ??? ??? ??? ?} ?? ??? ??? ??? ?return new Promise((resolve) => { ?? ??? ??? ??? ??? ?API.getNotice(data).then(res => { ?? ??? ??? ??? ??? ??? ?console.log(res) ?? ??? ??? ??? ??? ??? ?if (res.data.code == "200") { ?? ??? ??? ??? ??? ??? ??? ?this.noticeList = res.data.data.list ?? ??? ??? ??? ??? ??? ??? ?console.log(this.noticeList) ?? ??? ??? ??? ??? ??? ??? ?resolve(); ?? ??? ??? ??? ??? ??? ??? ?return ?? ??? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ??? ?uni.showToast({ ?? ??? ??? ??? ??? ??? ??? ??? ?title: res.data.message, ?? ??? ??? ??? ??? ??? ??? ??? ?duration: 1000, ?? ??? ??? ??? ??? ??? ??? ??? ?icon: "none" ?? ??? ??? ??? ??? ??? ??? ?}) ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?}) ?? ??? ??? ??? ?})?? ??? ??? ??? ? ?? ??? ??? ?},
第二個異步方法
//獲得當前的公告列表 ?? ??? ??? ?getNowNotice(){ ?? ??? ??? ??? ?//獲取當前時間戳 ?? ??? ??? ??? ?var timestamp = (new Date()).getTime(); ?? ??? ??? ??? ?var _this = this ?? ??? ??? ??? ?console.log(timestamp); ?? ??? ??? ??? ?//將noticeList的結束時間轉換成時間戳 ?? ??? ??? ??? ?for(var i=0; i<this.noticeList.length; i++){ ?? ??? ??? ??? ??? ?var endTimeStamp = TIME.TimeToTimeStamp(this.noticeList[i].endTime) ?? ??? ??? ??? ??? ?console.log(endTimeStamp) ?? ??? ??? ??? ??? ?if(endTimeStamp>timestamp){ ?? ??? ??? ??? ??? ??? ?_this.noticeNewList.push(this.noticeList[i]) ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ??? ?console.log("noticeNewList",_this.noticeNewList) ?? ??? ??? ?}
用async和await
async onLoad(option) { ?? ??? ??? ?await this.getAllNotice() ?? ??? ??? ?await this.getNowNotice() ?? ??? ?},
原文鏈接:https://blog.csdn.net/alan_alei/article/details/96995147
相關推薦
- 2023-08-01 在 TypeScript 中 interface 和 type 的區別
- 2022-02-25 FastDFS 端口映射問題,---spring改變jar包中bean方法邏輯的另一種特殊方式
- 2023-07-07 Tomcat文件夾屬性
- 2022-05-12 Python繪制計算機CPU占有率變化的折線圖_python
- 2022-07-07 C#操作注冊表之Registry類_C#教程
- 2024-02-28 CSS,文本溢出顯示省略號
- 2022-11-09 Oracle如何在SQL語句中對時間操作、運算_oracle
- 2022-03-19 好用的C++?string?Format“函數”介紹_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支