1. Introduction

Wechat apPLETS API [wx.login, wx.request, Wx. getUserInfo, wx.getSetting, etc.] all have success and FAIL callback methods and the conclusion can be encapsulated by proxy mode +Promise

2. Implementation idea

/ * * *@author Goldenrain tree *@date The 2021-03-31 *@description {proxy mode} 7 July *@param {Function} Fn wechat apPLETS API (wx.login, wx.request, wx.getUserInfo, wx.getSetting), etc. */

const promisic = (fn) = > {
  return (params = {}) = > {
    return new Promise((resolve, reject) = > {
      const args = Object.assign(params, {
        success: (data) = >resolve(data),
        fail: (error) = > reject(error)
      })
      // Call wechat API through proxy modefn(args); }}})/ / export
export {
  promisic
}
Copy the code

Use 3.

import { promisic } from './proxy';// the method is used in the proxy file to export methods

// For example, we call wx.request on our daily interface
class User {

  async getList() {
    const res = await promisic(wx.request)({
      url: "https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html".data: {
        author: 'goldenrain tree'.age: 24}})returnres; }}Copy the code

series

  • 1. A daily article, the first article of wechat mini program “Catalog”
  • 2. One article of the day, the second article of wechat mini program “Agent Mode”