下载es6-promise.min.js存放在utilslibs中
在utils中创建Promisify.js文件
Promisify.js文件代码
var Promise = require('libs/es6-promise.min')
function Promisify(fn) {
return function(obj = {}) {
return new Promise((resolve, reject) => {
obj.success = function(res) {
resolve(res)
}
obj.fail = function(res) {
reject(res)
}
fn(obj)
})
}
}
module.exports = {
Promisify: Promisify
}
使用方法
import { Promisify } from '../../utils/Promisify';
const request = Promisify(wx.request);
request({
url: '',
method: 'GET',
data: {
type:'page',
module:'contact'
},
header: {
'content-type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
},
})
.then(function (res) {
that.setData({
items: res.data
})
}