需求:由于页面请求查询数据并赋值,但所需赋值的选择框数据由几个接口查询获取,需要先执行完选择框接口查询,再执行数据查询
链式调用
如果只有单个选择框查询,可以在接口查询完成后成功回调函数中执行数据查询
// 获取发布地市与发放渠道字典
function getRightActivityChoose () {
$.ajax({
url: url,
method: "post",
data: JSON.stringify({}),
dataType: "json",
contentType:"application/json;charset:UTF-8",
success: function (res) {
//查询内容详情
getActivityDetail();
},
error: function (data, type, err) {
showToast(err);
}
});
}
Promise方法
//确认办理
confirmHandle() {
//如果没有流水号就去调用流水号获取方法
if (this.orderId === "") {
this.createConcertNo().then(()=>{
this.doHandleVIP();
});
}else{
this.doHandleVIP();
}
},
//获取流水号
createConcertNo() {
return new Promise((resolve, reject) => {
this.$loading.showLoading();
createConcertNo({})
.then((res) => {
this.$loading.closeLoading();
if (res.resCode == "000000") {
this.orderId = res.busiDataResp.orderId?res.busiDataResp.orderId:'';
resolve();
} else {
reject();
this.$toast.showToast(res.resMsg);
}
})
.catch((err) => {
reject();
this.$toast.showToast(err);
});
})
},
回调函数
//确认办理
confirmHandle() {
//如果没有流水号就去调用流水号获取方法
if (this.orderId === "") {
this.createConcertNo(this.doHandleVIP);
}else{
this.doHandleVIP();
}
},
//获取流水号
createConcertNo(callback) {
this.$loading.showLoading();
createConcertNo({})
.then((res) => {
this.$loading.closeLoading();
if (res.resCode == "000000") {
this.orderId = res.busiDataResp.orderId?res.busiDataResp.orderId:'';
callback();
} else {
this.$toast.showToast(res.resMsg);
}
})
.catch((err) => {
this.$loading.closeLoading();
this.$toast.showToast(err);
});
},
san框架
this.getAddressLst().then(() =>{
this.queryCartSelectedLst().then(() => {
});
});
多个异步调用执行完成再执行某调用
jQuery ajax处理
使用 async:false,将接口调用变成同步执行,在执行完所有字典查询后再执行数据查询
promise 处理
多个函数执行完成再执行其他函数
getData1() {
return new Promise((resolve, reject) => {
request1(requestParams).then((res) => {//接口1
xxx
resolve(res);
}).catch(e =>{
reject(e)
})
});
},
getData2() {
return new Promise((resolve, reject) => {
request2(requestParams).then((res) => {//接口2
xxx
resolve(res);
}).catch(e =>{
reject(e)
})
});
},
getData() {
this.addLoading()//开始loading
Promise.all([
this.getData1(),
this.getData2()
]).then(res => {
console.log(res);
setTimeout(() => {
this.hideLoading()//延时结束loading
}, 800);
})
}
我的博客主页 :https://qingmuzhang.gitee.io/