vue循環(huán)中調(diào)用接口-promise.all();按順序執(zhí)行異步處理方式
vue循環(huán)調(diào)用接口-promise.all();按順序執(zhí)行異步處理
在vue中循環(huán)調(diào)用接口-promise.all()
methods: {
? handleAdd (arr) {
? ? ?this.loading = true
? ? ?const allApi = []
? ? ?arr.forEach((item, index) => {
? ? ? ?const data = {
? ? ? ? ?id: item.id,
? ? ? ? ?name: item.name
? ? ? ?}
? ? ? ?const oneApi = api.add(data).then(res => {
? ? ? ? ?if (res.error_code === 0) {
? ? ? ? ? ?this.$message.success(res.msg)
? ? ? ? ?} else {
? ? ? ? ? ?this.$message.error(res.msg)
? ? ? ? ?}
? ? ? ?})
? ? ? ?allApi.push(oneApi)
? ? ?})
? ? ?Promise.all(allApi).then(() => {
? ? ? ?this.loading = false
? ? ?})
? ?}
}有異步處理數(shù)據(jù)時,按順序執(zhí)行函數(shù)
methods: {
?? ?handleAdd (val) {
? ? ? ? this.addTag(val).then(() => {
? ? ? ? ? this.tags.push(this.newTag)
? ? ? ? ? this.editNote()
? ? ? ? })
? ? },
?? ?addTag (val) {
? ? ? const data = {
? ? ? ? tag: val
? ? ? }
? ? ? return add(data).then(res => {
? ? ? ? this.newTag = {
? ? ? ? ? id: res.data.id,
? ? ? ? ? name: res.data.name
? ? ? ? }
? ? ? })
? ? },
? ? editNote () {
? ? ? const data = {
? ? ? ? tags: this.tags,
? ? ? }
? ? ? update(data).then((res) => {
? ? ? ? if (res.error_code === 0) {
? ? ? ? ? this.$message.success('修改成功!')
? ? ? ? ?}
? ? ? })
? ? }
}使用return返回第一個異步處理的結(jié)果;使用then,繼續(xù)執(zhí)行第二個異步處理(當?shù)谝淮畏祷亟Y(jié)果為ture時)。
vue中Promise.all的使用
Promise.all
簡述
Promise.all可以將多個Promise實例包裝成一個新的Promise實例。同時,成功和失敗的返回值是不同的,成功的時候返回的是一個結(jié)果數(shù)組,而失敗的時候則返回最先被reject失敗狀態(tài)的值。
Promse.all在處理多個異步處理時非常有用,比如說一個頁面上需要等兩個或多個ajax的數(shù)據(jù)回來以后才正常顯示,在此之前只顯示loading圖標。
注意: Promise.all成功結(jié)果數(shù)組里的數(shù)據(jù)順序和Promise.all接收到的數(shù)組順序是一致的。
所以在前端開發(fā)請求數(shù)據(jù)的過程中,偶爾會遇到發(fā)送多個請求并根據(jù)請求順序獲取和使用數(shù)據(jù)的場景,使用Promise.all毫無疑問可以解決這個問題。
舉例
let P1 = new Promise((resolve, reject) => {
resolve('成功')
})
let P2 = new Promise((resolve, reject) => {
resolve('success')
})
let P3 = Promse.reject('失敗')
Promise.all([P1, P2]).then((result) => {
console.log(result) //控制臺打印結(jié)果['成功了', 'success']
}).catch((error) => {
console.log(error)
})
Promise.all([P1,P3,P2]).then((result) => {
console.log(result)
}).catch((error) => {
console.log(error) // 控制臺打印結(jié)果 '失敗'
})實戰(zhàn)
這里實現(xiàn)的功能是調(diào)用后臺接口返回數(shù)據(jù)實現(xiàn)全選反選

<template>
<div class="table-container-wapper" id="apps-permission" v-loading="isTableLoading">
<div class="func-btn">
<el-button @click="selectInvert" class="invert">反選</el-button>
<span class="cur">/</span>
<el-button @click="selectAll" class="allSelect">全選</el-button>
</div>
<div class="choose">
<div v-for="(item, index) in form" :key="index" class="select-list">
<el-checkbox v-model="item.select">{{ item.serviceName }}</el-checkbox>
</div>
</div>
<div class="foot">
<el-button class="cancel" size="small" @click="$router.back()">取 消</el-button>
<el-button type="success" size="small" @click="submit">確 定</el-button>
</div>
</div>
</template><script>
import BaseMixin from "@/mixins/base";
import request from "@/utils/request";
import SETTING from "@/settings";
export default {
mixins: [BaseMixin],
data() {
return {
clientId: this.$route.query.id,
form: [],
};
},
created() {
this.isTableLoading = true
Promise.all([
this.getServiceInfo(),
this.getList()
]).then(([form, data]) => {
let hasArr = data.map(item => item.serviceId)
form.forEach(item => {
if(hasArr.includes(item.id)) {
item.select = true
}else {
item.select = false
}
})
this.form = form
this.isTableLoading = false
}, _ => {
this.isTableLoading = false
})
},
methods: {
getServiceInfo() {
return new Promise((resolve, reject) => {
request({
url: `${SETTING.IOT_APPLICATION}/serviceInfo`,
params: {
page: this.pagination.page - 1,
size: 1000,
},
}).then(
(res) => {
if (res.code=== "200") {
resolve(res.data.content)
}
reject()
},
(_) => {
reject()
this.$message({
type: "error",
message: _.message || "查詢列表失敗",
});
}
);
})
},
getList() {
return new Promise((resolve, reject) => {
request({
url: `${SETTING.IOT_APPLICATION}/sdkAppServiceRelation/curRights/${this.clientId}`,
}).then(
(res) => {
if (res[SETTING.IOT_THING_MODEL_STATES] === "200") {
resolve(res.data)
}
reject()
},
(_) => {
reject()
this.$message({
type: "error",
message: _.message || "查詢列表失敗",
});
}
);
})
},
//全選
selectAll() {
console.log(111)
this.form.forEach((item) => {
item.select = true;
});
},
//反選
selectInvert() {
this.form.forEach((item) => {
item.select = !item.select;
});
},
//提交
submit() {
let ids = this.form.filter(item => item.select).map(item => item.id)
request({
url: `${SETTING.IOT_APPLICATION}/sdkAppServiceRelation/rights`,
method: "post",
data: {
clientId: this.clientId,
ids: ids
}
}).then(
(res) => {
if (res[SETTING.IOT_THING_MODEL_STATES] === "200") {
this.$message({
type: "success",
message: res.msg || res.message || "操作成功",
});
this.$router.back()
}
},
(_) => {
reject()
this.$message({
type: "error",
message: _.message || "查詢列表失敗",
});
}
);
},
},
};
</script><style lang="scss" scope>
#apps-permission {
max-width: 1000px;
.func-btn {
overflow: hidden;
margin-top: 10px;
.invert {
border: 0px;
padding: 0;
float: right;
font-size: 16px;
}
.cur {
margin-left: 5px;
margin-right: 5px;
float: right;
font-size: 16px;
}
.allSelect {
border: 0px;
padding: 0;
float: right;
font-size: 16px;
}
}
.choose {
display: flex;
flex-wrap: wrap;
.select-list{
margin-bottom: 12px;
width: 25%;
}
}
.foot {
display: flex;
justify-content: flex-end;
margin-top: 20px;
}
}
</style>擴展知識:Promise.race,哪個結(jié)果快就返回哪個
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue+electron實現(xiàn)創(chuàng)建多窗口及窗口間的通信(實施方案)
這篇文章主要介紹了vue+electron實現(xiàn)創(chuàng)建多窗口及窗口間的通信,本文給大家分享實施方案結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-09-09
詳解vue的數(shù)據(jù)劫持以及操作數(shù)組的坑
這篇文章主要介紹了vue的數(shù)據(jù)劫持以及操作數(shù)組的坑,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vue實現(xiàn)把頁面導(dǎo)出成word文件的方法
這篇文章主要為大家詳細介紹了vue實現(xiàn)把頁面導(dǎo)出成word文件的方法,文中的實現(xiàn)步驟講解詳細,并且有詳細的代碼示例,需要的小伙伴可以參考一下2023-10-10

