#青训营 x 字节后端训练营# module.exports = function(path, data, method) {
return new Promise((resolve, reject) => {
wx.request({
url: '', // 接口地址
method: method,
data: data,
header: {'Content-Type': 'json' },
success: function(res) { // 请求成功执行resolve操作
resolve(res.data);
},
fail: function() { // 请求失败执行fail操作
reject()
wx.showModal({ showCancel: false, title: '失败' })
}
})
})
}
在请求成功后,调用 resolve 函数并传入响应数据,Promise 的状态会变为已解决(fulfilled),并将响应数据传递给下一个 then 函数。