137-3385-8848(同微信)

微信小程序 wx.request 方法的不支持 Promise 和并发数问题的解决方案

2022-04-21 管理员

 

wx-promise-request 是微信小程序 wx.request 方法的不支持 Promise 和并发数问题的解决方案。如果只需要解决并发数问题,可以使用我的 wx-queue-request

解决问题

  • 支持 Promise (使用 es6-promise 库)。
  • 管理请求队列,解决 request 最大并发数超过 10 会报错的问题。

下载

由于小程序不支持 npm,所以直接右键保存 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 的行为。

属性 说明 类型 默认值
request 发起网络请求的函数 Function wx.request
Promise Promise 函数 Function es6-promise
concurrency 最大并发数 number 10
import {request, setConfig} from './wx-promise-request';
import qcloud from './vendor/qcloud-weapp-client-sdk/index';
import Promise from 'bluebird';

// 根据自身需求,来定制 request
setConfig({
    request: qcloud.request, // 使用 qcloud 提供的请求方法
    Promise, // 使用 bluebird 作为 Promise
    concurrency: 5, // 限制最大并发数为 5
})

request({
  url: 'test.php',
})
.then(res => console.log(res))
.catch(error => console.log(error));

 

更多小程序开发教程入门关注。
上一篇:小程序定位怎么添加位置
下一篇:微信小程序获取appid在哪
相关资讯 Releva ntnews
解决方案 Solutions
相关热点 Hot spot
郑州做排名中正确使用关键词优化的方法
  1. 我们的优势
  2. 我们的实力
  3. 选择我们的理由
咨询电话(微信同号)

137-3385-8848(同微信)

豫ICP备16014343号-1

Copyright © 2017-2022 版权所有 酷微科技 Rights Reserved

电话咨询 在线咨询 服务项目 SEO优化