// 一、以请求为例
wx.request({
url: 'test.php', //仅为示例,并非真实的接口地址
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json' // 默认值
},
success (res) {
console.log(res.data)
}
})
// 二、promise化
return new Promise((res,rej)=>{
wx.request({
url: 'test.php',
success (ok) {
res(ok.data)
},
fail(err) {
rej(err)
}
})
})
// 三、依照二抽象一下 promise (代理模式)
const promises = (fc) => {
return (argus) => {
return new Promise((res,rej)=>{
const parms = {
...argus,
success (ok) { res(ok.data) },
fail (err) { rej(err) }
}
fc(parms)
})
}
}
// 四、新建小程序test
// ....
promises(wx.getStorage)({
key: 'logs',
}).then(res=>{
console.log('promises',res);
})
微信小程序api Promise化
最新推荐文章于 2023-10-30 09:44:59 发布