Introducing Promises in WeChat Mini Programs

Today's front-end developers are basically used to Promises, but they don't know why the WeChat applet doesn't support it! not support! not support! (Important said three times)

Then there is no way, we can only introduce it manually;

I use bluebird here, others can copy my method;

npm install bluebird --save

Then you can import the file, like me

const Promise = require('../libs/bluebird.min')

Then you can use Promises in the file, for example:

export function GET_REQ (url) {
  return new Promise((reslove, reject) => {
    wx.request({
      url: domain + url,
      method: 'GET',
      success: function (res) {
        reslove(res)
      },
      fail: function (err) {
        reject(`网络连接失败${err}`)
      }
    })
  })
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324982076&siteId=291194637