網站首頁 編程語言 正文
- 本文介紹一下Promise.all() 的常用場景
Promise 有好幾個api, 這篇文章主要是講 .all的常用場景
// api 方法一
getMethodsFirst(){
return new Promise((resolve, inject)=>{
// 調用一個后端接口,此處是調用已經封裝好了的axios,所以大家可以根據自己項目中的接口方法靈活訪問
getMethodsFirstApi().then((res:any)=>{
resolve(res) // 返回獲取到的正確的數據
},(req:any)=>{
inject(req) //返回失敗信息
})
})
}
// api 方法二
getMethodsSecond(){
return new Promise((resolve, inject)=>{
// 方法二的后端api調用接口
getMethodsSecondApi().then((res:any)=>{
resolve(res) // 返回獲取到的正確的數據
},(req:any)=>{
inject(req) //返回失敗信息
})
})
}
init(){
// 需要等到getMethodsFirst方法和getMethodsSecond方法都返回數據后再執行
Promise.all([this.getMethodsFirst(), this.getMethodsSecond()]).then((res:any)=>{
// res 接收的是一個數組,.all()方法中請求幾個方法,數組的值就有幾個
// res[0] 是this.getMethodsFirst()的返回成功的值 , res[1] 是this.getMethodsSecond()的返回成功的值
// 接收到返回值之后,就可以做自己想要做的事情了
})
}
原文鏈接:https://blog.csdn.net/qq_35257117/article/details/105486198
相關推薦
- 2022-05-13 顯示類型轉換 const_cast, static_cast, dynamic_cast, tein
- 2022-03-23 C++虛函數注意事項_C 語言
- 2022-12-09 C++中使用cout以hex格式輸出方式_C 語言
- 2022-06-12 python下grpc與protobuf的編寫使用示例_python
- 2022-10-09 C#實現線性查找算法_C#教程
- 2022-07-14 Matlab實現灰色預測的示例代碼_C 語言
- 2022-10-14 Go?Ginrest實現一個RESTful接口_Golang
- 2021-11-26 Linux下查看IP地址不顯示解決辦法_Linux
- 最近更新
-
- 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同步修改后的遠程分支