本次分享学习内容:
1、如何在方法中调用方法,并等待调用的方法执行完后再继续执行
2、代码
TableItem() {//调用方法的方法---uniqueData:循环的数据
this.getDescProcess(uniqueData, item) //退旧情况编码进行描述
},
async getDescProcess(item) { //进行对this.getDescData异步调用
try {
const data = await this.getdesc(item)
return data
} catch (error) {
console.error('发生错误', error)
}
},
// 处理字典---调用的方法使用promise请求
getdesc(item) {
return new Promise((resolve, reject) => {
//请求接口的数据
this.$api.user.getDictionaryWZ(uni.getStorageSync('userInfo')
.US_COMPANY_ID, item)
.then(res => {
resolve(res)
})
})
}
总结:
方法中,调用异步方法的方法前加上async
async getDescProcess(item){}
this.getdesc(item)方法前添加await,表示等待这个方法执行。
同时,this.getdesc()方法要是用Promise的形式,请求完数据后reslove(res)