//封装的方法
requestFunction:function(url,params){
var obj;
wx.request({
url: url,
data: params,
method: 'get',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: function (result) {
console.log("返回值:"+result.data)
obj = result.data
}
})
return obj;
}
----------------但是现在的问题,这个是异步请求,我在调用方法获取到这个返回值的时候,返回值还没被赋值,大神们有没有啥解决方法
----------------不会同步。。。

网络请求是异步的,直接同步返回肯定出错。想省事,就把你这个方法加一个参数——回调函数即可。
requestFunction:function(url,params,callback){wx.request({url: url,data: params,method:'get',header: {'content-type':'application/x-www-form-urlencoded'},success:function(result) {console.log("返回值:"+result.data)callback(result.data)}})}import regeneratorRuntime from'../utils/regenerator-runtime/runtime-module'const wxRequest = async (url, params = {}) => {Object.assign(params, {token: wx.getStorageSync('token')})let data = params.query || {}wx.showLoading({title:'加载中...',})let res = awaitnewPromise((resolve, reject) => {wx.request({url: url,method: params.method ||'GET',data: data,header: {'Content-Type':'application/json','token': params.token ||''},success: (res) => {if(res && res.statusCode == 200 && res.data) {resolve(res.data)}else{reject(res)}},fail: (err) => {wx.showModal({title:'网络出错,请稍后重试',showCancel:true})reject(err)},complete: (e) => {wx.hideLoading()}})})returnres}module.exports = {wxRequest}用了async/await。仅供参考。
谢大佬,可我不咋看得懂。。。。我用了Promise之后返回值是这样
Promise { [[PromiseStatus]] : "pending" , [[PromiseValue]] : undefined }
__proto__ : Promise
[[PromiseStatus]] : " resolved "
[[PromiseValue]] : " 测试i同步 "
请问我怎么拿到value
。。。。用上了promise后你都要用异步的方法。。
你自己先去看看promise的用法吧
。。。。用上了promise后你都要用异步的方法。。
你自己先去看看promise的用法吧。
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise
现在小程序能使用 async了?
import regeneratorRuntime from'../utils/regenerator-runtime/runtime-module'引入这个。。
我是这样封装的
/*** 封装 post 请求为 Promise* @param url* @param data* @param header* @returns {*|Promise<any>}*/post:function(url, data, header = {}) {if(this.globalData.deviceSerialNumber) {header = {...header,'device-serial-number':this.globalData.deviceSerialNumber};}returnurl && data &&newPromise((resolve, reject) => {wx.request({url: url,method:'POST',data: data,header: {'content-type':'application/json',// 微信默认就是...header},success:function(res) {resolve(res);},fail:function(res) {reject(res);}});});}Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
__proto__:Promise
[[PromiseStatus]]:"resolved"
[[PromiseValue]]:"测试i同步"
我封装后获取的返回值是这种的。。。请问我怎么才能拿到value,我返回的值。。
console.log("准备测试")
var cc =
app.requestFunction("http://localhost:8080/selectTest.do", new Object(id = 12));
console.log(cc)
console.log("结束测试")
我是这样测试的
这个不是 async,是 Promise 的写法
嗯嗯,好的好的,谢谢,已经解决了,,
碰到和你一样的问题 你是怎么解决的?
从外面传个回调方法进来?或者可以考虑使用 Promise
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
__proto__:Promise
[[PromiseStatus]]:"resolved"
[[PromiseValue]]:"测试i同步"
我封装后获取的返回值是这种的。。。请问我怎么才能拿到value,我返回的值。。
console.log("准备测试")
var cc =
app.requestFunction("http://localhost:8080/selectTest.do", new Object(id = 12));
console.log(cc)
console.log("结束测试")
我是这样测试的
promise的基本用法哎,你最好百度下。
requestFunction里要调在成功后用 resolve。
var cc = app.requestFunction( "http://localhost:8080/selectTest.do" , new Object(id = 12 )).then((value)=>{
// 这里是 resolve之后进来的
})
嗯嗯,我参数传递成功了,只是刚才想省一行,没想到这样不能传,又改回来了
嗯嗯,我参数传递成功了,只是刚才想省一行,没想到这样不能传,又改回来了
success: function (result) {
console.log("返回值" + result.data)
resolve(result.data)
},
fail: function (result){
reject(result.data)
}
我在方法里这样写的 --------------
返回值就是这种
Promise { [[PromiseStatus]] : "pending" , [[PromiseValue]] : undefined }
__proto__ : Promise
[[PromiseStatus]] : " resolved "
[[PromiseValue]] : " 测试i同步 "
祝通,问题解决后的代码能不能贴出来让大家参考一下,从上看到下,不清楚你是怎么解决的,谢谢。
呃。。。大佬。。我没解决啊。。。还是不会。。这个问题就先放下了。。不是不发。。
还以为你的问题已经解决了呢,关键是微信官方没有给相应的示例,让大家在这里摸索
目前我了解的,好像就 promise 和 回调函数 但都不会用...
你可以尝试了解下这个https://github.com/mushan0x0/wxapp-api-interceptors
参考:https://www.jianshu.com/p/edd9a1aac8bd
这种调用方式就很有问题吧,网络请求本来就是异步的,为什么要同步获取结果?
因为有的业务的确是需要先获取服务器数据再处理,所以要拿到数据再继续处理,所以只能同步
// 请求公共数据使用// 仅供参考,return返回的是requestTask对象
request:function({ path, params = {}, cb, method ='GET', header = {'Accept':'application/json'}, fail }) {varthat =thisreturnwx.request({url: that.yuming + path,method: method,data: params,header: header,success:function(res) {typeofcb =="function"&& cb(res)},fail:function(err) {that.hideLoading()console.log('err', err)typeoffail =="function"&& fail(err)if(err.errMsg =="request:fail abort")returnwx.showToast({title:'请求出错,请稍候再试',image:'/images/err.png',duration: 1000,mask:true,})}})},