promise.all的实际运用
表单校验时使用
- 页面存在多个表单验证,所有表单验证通过后,才进行操作
saveDetail () {
const infoForm = this.$refs['merchantInfoForm'].validate();
const settleVal = this.$refs['settleForm'].validate();
Promise.all([infoForm, settleVal]).then(res => {
if (res.every(item => item)) {
this.SaveMerchantsDetails();
}
})
},
接口请求时使用
- 使用场景:首先请求字典项接口,当所有字典项接口请求完毕,才开始请求信息接口
created() {
this.getMasterDicts();
},
methods: {
getMasterDicts() {
let p1 = new Promise((resolve, reject) => {
// 证件类型
getMasterDicts("card_type").then((res) => {
if (res.code === 200) {
this.cardTypeOptions = res.data || [];
this.cardTypeOptions.map((item) => {
item.name = item.dictLabel;
});
resolve();
}
});
});
let p2 = new Promise((resolve, reject) => {
// 学历
getMasterDicts("education").then((res) => {
if (res.code === 200) {
this.educationOptions = res.data || [];
this.educationOptions.map((item) => {
item.name = item.dictLabel;
});
resolve();
}
});
});
let p3 = new Promise((resolve, reject) => {
// 学位
getMasterDicts("degree").then((res) => {
if (res.code === 200) {
this.degreeOptions = res.data || [];
this.degreeOptions.map((item) => {
item.name = item.dictLabel;
});
resolve();
}
});
});
let p4 = new Promise((resolve, reject) => {
// 职称
getMasterDicts("certificate").then((res) => {
if (res.code === 200) {
this.certificateOptions = res.data || [];
this.certificateOptions.map((item) => {
item.name = item.dictLabel;
});
resolve();
}
});
});
let p5 = new Promise((resolve, reject) => {
// 执业类别
getMasterDicts("project_property").then((res) => {
if (res.code === 200) {
this.projectPropertyOptions = res.data || [];
resolve();
}
});
});
Promise.all([p1, p2, p3, p4, p5]).then((res) => {
this.getUserInfo();
});
},
}
网友评论