小程序
小游戏
企业微信
微信支付
扫描小程序码分享
如下所示是callback风格,如果改写成promise风格,那就是在.get()后面加上then(res=>{}),但是这个then里面的函数应该是get成功时调用的吧?如果get失败了,该执行的动作写在哪里呢?
不好意思,问题比较低级,初学中,还请各位大佬不吝赐教,谢谢
2 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
xxx.get().then(res=>{ console.log(res) }).catch(e=> { console.error(e) }) 或者 try{ let res = await xxx.get() console.log(res) }catch(e){ console.error(e) } callback风格,失败时的函数写fail xxx.get({ //success: function(res){} //success: res=>{} success(res){ 如果是 xxx.doc().get() => res.data 为单条数据记录{_id: 'xxxxx', ...} 如果是 xxx.where().get() => res.data 为数据记录数组[{_id: 'xxxxx', ...}, ...] }, //fail: function(e){} //fail: e=>{} fail(e){ // 不用管e格式, } })
若认为该回答有用,给回答者点个[ 有用 ],让答案帮助更多的人
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
.then(res=>{ }).catch(err => { })
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
xxx.get().then(res=>{ console.log(res) }).catch(e=> { console.error(e) }) 或者 try{ let res = await xxx.get() console.log(res) }catch(e){ console.error(e) } callback风格,失败时的函数写fail xxx.get({ //success: function(res){} //success: res=>{} success(res){ 如果是 xxx.doc().get() => res.data 为单条数据记录{_id: 'xxxxx', ...} 如果是 xxx.where().get() => res.data 为数据记录数组[{_id: 'xxxxx', ...}, ...] }, //fail: function(e){} //fail: e=>{} fail(e){ // 不用管e格式, } })
若认为该回答有用,给回答者点个[ 有用 ],让答案帮助更多的人
.then(res=>{ }).catch(err => { })
另外,callback风格,失败时的函数到底是写fail,还是写failure,然后成功和失败时返回的参数到底包含哪些,这要在哪查啊,我在开发文档里面找了半天也没找到啊