您的位置:首页 > 移动开发 > 微信开发

小程序中将异步请求变成同步请求

2020-07-14 05:56 267 查看

使用Promise

//图片上传
uploadimg:function(a){
var tt = this
return new Promise((resolve, reject) =>{
wx.uploadFile({
filePath: a,
name: 'file',
url: httpsRequest.httpsUrl + resHttp.fileImgIsVideo,
header: {
"content-type": "application/x-www-form-urlencoded",
"requesttype": "wxapp",
"token": tt.data.tokens
},
success(res) {
//resolve作用提示小程序请求已结束
resolve(JSON.parse(res.data))
},
fail (err) {
reject(err)
}
});
});
},
// 提交数据
//async关键词
async submit(e) {
var that = this,file_url = '';
//res为上方法请求返回的res.data内容
var res = await that.uploadimg(that.data.ImgsArr[0])
wx.request({
url: httpsRequest.httpsUrl + resHttp.feedback,
data: {},
method: "post",
header: {
'content-type': 'application/x-www-form-urlencoded',
"requesttype": "wxapp",
"token": that.data.tokens
},
success(res) {
console.log(res);
wx.showToast({
title: res.data.msg,
icon: 'success'
})
}
})

},
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: