1、下载facebook的regeneratorRuntime
npm i regeneratorRuntime
把node_modules\regenerator-runtime路径下的runtime-module.js和runtime.js文件放到小程序开发文件内
2、在wxRequest和使用async await的js文件引入
import regeneratorRuntime from '../regenerator-runtime/runtime-module'
3、封装wxRequest,让它支持async-await
import regeneratorRuntime from '../regenerator/runtime-module';
const wxRequest = async (url, data = {}, method, hideLoading) => {
//默认带上openid
Object.assign(data, {
openid: app.globalData.openid
});
let header = {
'Content-Type': 'application/x-www-form-urlencoded',
};
//默认使用POST方式
method = method || 'POST';
// hideLoading可以控制是否显示加载状态
if (!hideLoading) {
wx.showLoading({
title: '加载中...',
})
};
let res = await new Promise((resolve, reject) => {
wx.request({
url: url,
method: method,
data: data,
header: header,
success: (res) => {
if (res && res.data && res.data.status == 200) {
resolve(res.data);
} else {
reject(res);
}
},
fail: (err) => {
reject(err);
},
complete: () => {
wx.hideLoading();
}
});
}).catch((e)=>{return e.data});
return res;
}
export {
wxRequest
}
4、使用async await的页面引入
import {
wxRequest
} from "../../../../utils/wxRequest";
import regeneratorRuntime from '../../../../utils/regenerator/runtime-module';