微信小程序 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));
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短