微信小程序 wx.request方法的異步封裝實例詳解
更新時間:2017年05月18日 17:01:45 作者:Rolan
這篇文章主要介紹了微信小程序 wx.request方法的異步封裝實例詳解的相關(guān)資料,需要的朋友可以參考下
wx-promise-request 是對微信小程序 wx.request 方法的異步封裝。
解決問題
支持 Promise (使用 es6-promise 庫)。
管理請求隊列,解決 request 最大并發(fā)數(shù)超過 10 會報錯的問題。
下載
npm install wx-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'; import qcloud from './vendor/qcloud-weapp-client-sdk/index'; import Promise from 'bluebird'; setConfig({ request: qcloud.request, Promise, }) request({ url: 'test.php', }) .then(res => console.log(res)) .catch(error => console.log(error));
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
javascript數(shù)據(jù)類型之原始類型詳解
這篇文章主要為大家介紹了javascript數(shù)據(jù)類型之原始類型詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06