日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Promise.all() 函數的常用場景

作者:讓時光到此為止。 更新時間: 2022-02-24 編程語言
  • 本文介紹一下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

欄目分類
最近更新