微信小程序 wx.request方法的异步封装实例详解
wx-promise-request是对微信小程序wx.request方法的异步封装。
解决问题
支持Promise(使用es6-promise库)。
管理请求队列,解决request最大并发数超过10会报错的问题。
下载
npminstallwx-promise-request
然后拷贝dist/index.js文件到你的小程序项目中。
使用
import{request}from'./wx-promise-request';
request({
url:'test.php',
data:{
x:'',
y:'',
},
header:{
'content-type':'application/json',
},
})
.then(res=>console.log(res))
.catch(error=>console.error(error))
API
setConfig(object)
通过setConfig配置wx-promise-request,如:使用qcloud提供的request方法;使用其他Promise库等等。
import{request,setConfig}from'./wx-promise-request';
importqcloudfrom'./vendor/qcloud-weapp-client-sdk/index';
importPromisefrom'bluebird';
setConfig({
request:qcloud.request,
Promise,
})
request({
url:'test.php',
})
.then(res=>console.log(res))
.catch(error=>console.log(error));
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!