需求:由于页面请求查询数据并赋值,但所需赋值的选择框数据由几个接口查询获取,需要先执行完选择框接口查询,再执行数据查询
链式调用
如果只有单个选择框查询,可以在接口查询完成后成功回调函数中执行数据查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| 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方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| 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); }); }) },
|
回调函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| 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框架
1 2 3 4
| this.getAddressLst().then(() =>{ this.queryCartSelectedLst().then(() => { }); });
|
多个异步调用执行完成再执行某调用
jQuery ajax处理
使用 async:false,将接口调用变成同步执行,在执行完所有字典查询后再执行数据查询
promise 处理
多个函数执行完成再执行其他函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| getData1() { return new Promise((resolve, reject) => { request1(requestParams).then((res) => { xxx resolve(res); }).catch(e =>{ reject(e) }) }); }, getData2() { return new Promise((resolve, reject) => { request2(requestParams).then((res) => { xxx resolve(res); }).catch(e =>{ reject(e) }) }); }, getData() { this.addLoading() Promise.all([ this.getData1(), this.getData2() ]).then(res => { console.log(res); setTimeout(() => { this.hideLoading() }, 800); }) }
|