SoFunction
Updated on 2025-03-02

Detailed explanation of Promise the WeChat applet interface and use the async function

Preface

The interface of the applet is still the same as the one used in the beginning. What if you want to use Promise+Async without using the framework in the applet?

Latest solutions for 2019

1. Promise the interface

Create a file first

const promisify = name => option => {
 return new Promise((resolve, reject) =>
  wx[name]({
   ...option,
   success: resolve,
   fail: reject,
  })
 )
}

const wxPro = new Proxy(wx, {
 get(target, prop) {
  return promisify(prop)
 }
})

export default wxPro

2. Use regeneratorRuntime to make applets compatible with async functions

existgithub project regeneratorDownload packages/regenerator-runtime/ in it.

If it is the latest version, an error will be reported after introduction:

Function is not a function....

Need to manually modify the source code:

Remove the last try-catch statement in the source code and change the beginning var runtime to var regeneratorRuntime.

If you do not want to modify it, you can directly download the source code of version 0.13.1.

at last

Introduce in the page you want to use:

import wxPro from './utils/'
import regeneratorRuntime from './utils/'

//
App({
 async onLaunch() {
  // ().then((res) => {
  //  (res)
  // })
  const result = await ()
  (result)
 },
 globalData: {
  userInfo: null
 }
})

This is OK. The only thing that is a bit troublesome is that every page you want to use must be introduced once.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.