vue實現(xiàn)2個接口同步執(zhí)行方式
更新時間:2025年08月01日 09:26:36 作者:cc蒲公英
案例1使用async/await結(jié)合Promise.all實現(xiàn)并行接口調(diào)用,案例2通過async/await順序執(zhí)行確保數(shù)據(jù)依賴,兩種方式適用于不同場景,合理選擇可提升效率
vue2個接口同步執(zhí)行方式
案例1
// 接口1的請求
const promise1 = axios.get('/api/data1')
// 接口2的請求
const promise2 = axios.get('/api/data2')
// 同步執(zhí)行兩個接口
Promise.all([promise1, promise2]).then(results =>{
// 對兩個接口返回的數(shù)據(jù)進(jìn)行操作
const result1 = results[0].data
const result2 = results[1].data
// ...
}).catch(error =>{
// 處理錯誤
})案例2
function fn(){
return new Promise((resolve,reject)=>{
let randomNum = parseInt(Math.random()*6+1);
console.log(randomNum);
if(randomNum>3){
resolve('買');
}
else{
reject('不買');
}
})
}
Promise.all([fn(),fn()]).then((x)=>{console.log(x,'success')},(y)=>{console.log(y,'error');});總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue單頁應(yīng)用加百度統(tǒng)計代碼(親測有效)
這篇文章主要介紹了vue單頁應(yīng)用加百度統(tǒng)計代碼的解決方法,需要的朋友參考下吧2018-01-01
Vue?Router路由hash模式與history模式詳細(xì)介紹
Vue?Router是Vue.js官方的路由管理器。它和Vue.js的核心深度集成,讓構(gòu)建單頁面應(yīng)用變得易如反掌。路由實際上就是可以理解為指向,就是我在頁面上點(diǎn)擊一個按鈕需要跳轉(zhuǎn)到對應(yīng)的頁面,這就是路由跳轉(zhuǎn)2022-08-08
Vue報錯error:0308010C:digital?envelope?routines::unsupported
這篇文章主要給大家介紹了關(guān)于Vue報錯error:0308010C:digital?envelope?routines::unsupported的解決方法,文中通過圖文將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11

