在微信小程序中使用 async await

https://segmentfault.com/a/1190000021966277

1、打开开发者工具的增强编译

2、在app.js中加上:

//接口请求地址
wx.$requestServe = "http://localhost:3087/employee_applet";

//async、await
function promisify(fn) {
  return async function (args) {
    return new Promise((resolve, reject) => {
      fn({
        ...(args || {}),
        success: (res) => resolve(res),
        fail: (err) => reject(err),
      });
    });
  };
}
function toAsync(...names) {
  return (names || [])
    .map((name) => ({ name, member: wx[name] }))
    .filter((t) => typeof t.member === "function")
    .reduce((r, t) => {
      r[t.name] = promisify(wx[t.name]);
      return r;
    }, {});
}
Promise.prototype.ignoreError = function () {
  return this.catch(() => {});
};
wx.$awx = toAsync("request");

App({
  onShow(option) {},
  globalData: {},
});

3、在页面中使用:

async onShow() {
  let res = await wx.$awx
    .request({
      url: `${wx.$requestServe}/compareFaceToIdCard`,
    })
    .ignoreError();
  console.log(res);
}

上一篇:restlet不能接受angular post过来的数据


下一篇:Android 实现蘑菇街购物车动画效果