promise用法
我想用promise让wx.request同步,代码如下,但是结果 aaaaa log先输出了,仍旧是异步,没有同步,我想知道我哪儿错了,求指点。
onLoad() {
var that = this;
that.getPromiseRequest().then(function(res){
that.setData({
fileCont: res
})
console.log(‘res is’+res) // 第一次的log
})
。。。
console.log(‘aaaaa’) //第二次的log
}
,
getPromiseRequest() {
return new Promise(function (resolve, reject) {
wx.request({
url: ‘xxx’,//请求地址
header: {//请求头
“Content-Type”: “application/x-www-form-urlencoded”
},
method: “GET”,//get为默认方法/POST
success: function (res) {
resolve(res.data);//将结果往上抛
},
fail: function (err) { },//请求失败
complete: function () { }//请求完成后执行的函数
})
})
},
