异步是前端的本质,在项目中,有些业务逻辑就需要用到同步执行
Page({
data: {
},
/**
* 批量导入
*/
muchclickimages() {
var that = this;
//选择文件(图片)
wx.chooseMessageFile({
count: 100,
type: 'image',
success(res) {
console.log(res)
//临时路径
// console.log(res.tempFiles)
main()//调用
async function main(){
for(var i =0;i<res.tempFiles.length;i++){
//await等待
await that.cloudGetPath(res.tempFiles[i].name)
}
}
})
},
/**
* 云函数通过名字获取地址
* @param {*} name
* return 路径
*/
cloudGetPath:function(name) {
var that = this
//return
return new Promise(resolve=>{
wx.cloud.callFunction({
name: "getStudentData",
data: {
name: name
}
}).then(res=>{
console.log(res)
//在执行完成后resolve
resolve()
})
})
},
onLoad() {
this.muchclickimages()
}
})
这样就可以解决异步的问题了