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ù)時(shí),按順序執(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返回第一個(gè)異步處理的結(jié)果;使用then,繼續(xù)執(zhí)行第二個(gè)異步處理(當(dāng)?shù)谝淮畏祷亟Y(jié)果為ture時(shí))。
vue中Promise.all的使用
Promise.all
簡(jiǎn)述
Promise.all可以將多個(gè)Promise實(shí)例包裝成一個(gè)新的Promise實(shí)例。同時(shí),成功和失敗的返回值是不同的,成功的時(shí)候返回的是一個(gè)結(jié)果數(shù)組,而失敗的時(shí)候則返回最先被reject失敗狀態(tài)的值。
Promse.all在處理多個(gè)異步處理時(shí)非常有用,比如說一個(gè)頁(yè)面上需要等兩個(gè)或多個(gè)ajax的數(shù)據(jù)回來以后才正常顯示,在此之前只顯示loading圖標(biāo)。
注意: Promise.all成功結(jié)果數(shù)組里的數(shù)據(jù)順序和Promise.all接收到的數(shù)組順序是一致的。
所以在前端開發(fā)請(qǐng)求數(shù)據(jù)的過程中,偶爾會(huì)遇到發(fā)送多個(gè)請(qǐng)求并根據(jù)請(qǐng)求順序獲取和使用數(shù)據(jù)的場(chǎng)景,使用Promise.all毫無疑問可以解決這個(gè)問題。
舉例
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) //控制臺(tái)打印結(jié)果['成功了', 'success'] }).catch((error) => { console.log(error) }) Promise.all([P1,P3,P2]).then((result) => { console.log(result) }).catch((error) => { console.log(error) // 控制臺(tái)打印結(jié)果 '失敗' })
實(shí)戰(zhàn)
這里實(shí)現(xiàn)的功能是調(diào)用后臺(tái)接口返回?cái)?shù)據(jù)實(shí)現(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>
擴(kuò)展知識(shí):Promise.race,哪個(gè)結(jié)果快就返回哪個(gè)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue+electron實(shí)現(xiàn)創(chuàng)建多窗口及窗口間的通信(實(shí)施方案)
這篇文章主要介紹了vue+electron實(shí)現(xiàn)創(chuàng)建多窗口及窗口間的通信,本文給大家分享實(shí)施方案結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09Vue 全家桶實(shí)現(xiàn)移動(dòng)端酷狗音樂功能
這篇文章主要介紹了Vue 全家桶實(shí)現(xiàn)移動(dòng)端酷狗音樂功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11詳解vue的數(shù)據(jù)劫持以及操作數(shù)組的坑
這篇文章主要介紹了vue的數(shù)據(jù)劫持以及操作數(shù)組的坑,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04vue實(shí)現(xiàn)把頁(yè)面導(dǎo)出成word文件的方法
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)把頁(yè)面導(dǎo)出成word文件的方法,文中的實(shí)現(xiàn)步驟講解詳細(xì),并且有詳細(xì)的代碼示例,需要的小伙伴可以參考一下2023-10-10vue通信方式EventBus的實(shí)現(xiàn)代碼詳解
這篇文章主要介紹了vue通信方法EventBus的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06