在vue項(xiàng)目中promise解決回調(diào)地獄和并發(fā)請(qǐng)求的問(wèn)題
場(chǎng)景需求:
需要同時(shí)請(qǐng)求5個(gè)接口
都請(qǐng)求成功后執(zhí)行下一步操作
解決方法:
定義一個(gè)變量i=5,請(qǐng)求成功一個(gè)接口,讓i–,直到i=0時(shí)執(zhí)行下一個(gè)操作,否則不執(zhí)行
axios.all 并發(fā)請(qǐng)求,.then(axios.spread(function(callback1, callback2)){})
promise.all 并發(fā)請(qǐng)求,.then(function([callback1, callback2]){})
1、回調(diào)地獄:
函數(shù)作為參數(shù)層層嵌套
代替的為.then的鏈?zhǔn)讲僮?/p>
2、promise.all并發(fā)請(qǐng)求
引入接口
import {getSellerDetail} from '../../api/seller'
import {getMemberCardInfo} from '../../api/pay_online/index'
數(shù)據(jù)處理
1. 創(chuàng)建一個(gè)Promise實(shí)例,獲取數(shù)據(jù)
2. 并把數(shù)據(jù)傳遞給處理函數(shù)resolve和reject
3. promise在聲明時(shí)就執(zhí)行了
created(){ if (this.$route.query.type){ this.type = this.$route.query.type; this.sellerId = this.$route.query.targetId; this.initApi() } }, methods: { initApi(){ `// 商戶信息` let SellerDetailApi = new Promise((resolve, reject) => { getSellerDetail(this.sellerId).then( res => { resolve(res) // resolve(res.data) }).catch( err => { reject(res) }) }) `// 會(huì)員卡信息` let MemberCardInfoApi = new Promise((resolve, reject) => { getMemberCardInfo(this.sellerId, this.payMoney).then( res => { resolve(res) // resolve(res.data) }).catch( err => { reject(res) }) }) `// Promise的all方法,等數(shù)組中的所有promise對(duì)象都完成執(zhí)行` Promise.all([SellerDetailApi, MemberCardInfoApi]).then( res => { this.loading = false; // 商戶信息 this.detail = res[0].data.detail; this.sellerPic = this.detail.picture; this.sellerName = this.detail.name; this.discount = this.detail.discount; // 會(huì)員卡信息 this.cardDetail = res[1].data; this.balance = this.cardDetail.balance; //余額 this.rechargeTip = this.cardDetail.rechargeTip; // 付款金額提示充值 }).catch( err => { console.log(err) }) } }
3、接口返回:
promise.all中console.log(res) 返回的是數(shù)組接口返回
4、注意:
Promise.all 缺陷 如果其中某個(gè)任務(wù)出現(xiàn)異常(reject),所有任務(wù)都會(huì)掛掉,Promise直接進(jìn)入 reject 狀態(tài)至catch回調(diào)。
Promise.allSettled 無(wú)論一個(gè)任務(wù)正?;蛘弋惓?,都會(huì)返回對(duì)應(yīng)的的狀態(tài),可以解決上述問(wèn)題
補(bǔ)充知識(shí):vue項(xiàng)目中Promise同步請(qǐng)求
1.js中定義Promise
export function wxLogin() { let pResult = new Promise((resolve, reject) => { uni.login({ provider: 'weixin', success: (res) => { console.log('login success:', res); // return res; setTimeout(function() { resolve(res); }, 3000); }, fail: (err) => { console.log('login fail:', err); reject(err); } }); }).catch(res => { console.log(666, res); }) return pResult; }
2.vue文件中使用
import {login,wxLogin} from '@/common/login.js' (async () => { //獲取授權(quán)狀態(tài) console.log(1111,"111") let aaa = await wxLogin(); console.log(3333,"3333"); console.log(4444,aaa); })()
以上這篇在vue項(xiàng)目中promise解決回調(diào)地獄和并發(fā)請(qǐng)求的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue?頂部消息橫向滾動(dòng)通知效果實(shí)現(xiàn)
系統(tǒng)頂部展示一個(gè)橫向滾動(dòng)的消息通知,就是消息內(nèi)容從右往左一直滾動(dòng),這篇文章主要介紹了vue頂部消息橫向滾動(dòng)通知,需要的朋友可以參考下2024-02-02Vue中的驗(yàn)證登錄狀態(tài)的實(shí)現(xiàn)方法
這篇文章主要介紹了Vue中的驗(yàn)證登錄狀態(tài)的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03簡(jiǎn)單學(xué)習(xí)vue指令directive
這篇文章主要和大家一起簡(jiǎn)單學(xué)習(xí)一下vue指令:directive,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11利用vue-i18n實(shí)現(xiàn)多語(yǔ)言切換效果的方法
這篇文章主要給大家介紹了關(guān)于利用vue-i18n實(shí)現(xiàn)多語(yǔ)言切換效果的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue-i18n具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Vue ElementUI this.$confirm async await封
這篇文章主要介紹了Vue ElementUI this.$confirm async await封裝方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Vue echarts畫(huà)甘特圖流程詳細(xì)講解
這篇文章主要介紹了Vue echarts畫(huà)甘特圖流程,甘特圖(Gantt chart)又稱為橫道圖、條狀圖(Bar chart)。其通過(guò)條狀圖來(lái)顯示項(xiàng)目、進(jìn)度和其他時(shí)間相關(guān)的系統(tǒng)進(jìn)展的內(nèi)在關(guān)系隨著時(shí)間進(jìn)展的情況2022-09-09極速上手 VUE 3 teleport傳送門(mén)組件及使用語(yǔ)法
teleport 傳送門(mén)組件,提供一種簡(jiǎn)潔的方式,可以指定它里面的內(nèi)容的父元素,也就是說(shuō)teleport 中的內(nèi)容允許我們控制在任意的DOM中,使用簡(jiǎn)單,對(duì)VUE 3 teleport傳送門(mén)相關(guān)知識(shí)感興趣的朋友一起看看吧2021-10-10