收藏
回答

这个函数怎么封装成promise?跪求大佬帮忙解决一下

const authorization = function authorization(app{
        let _this = this
            //需要用户同意授权获取自身相关信息
        return new Promise(() => {
            wx.getSetting({
                successfunction(res{
                    if (res.authSetting['scope.userInfo']) {
                        //将授权结果写入app.js全局变量
                        app.globalData.hasLogin = true
                            //从云端获取用户资料
                        wx.cloud.callFunction({
                            name'get_userinfo',
                            data: {
                                getSelftrue
                            },
                            successres => {
                                if (res.errMsg == "cloud.callFunction:ok" && res.result) {
                                    //如果成功获取到
                                    //将获取到的用户资料写入app.js全局变量
                                    app.globalData.userInfo = res.result.data.userData
                                    app.globalData.userId = res.result.data._id
                                        // wx.switchTab({
                                        //     url: '/pages/home/home'
                                        // })
                                } else {
                                    console.log("未注册")
                                }
                            },
                            failerr => {
                                wx.showToast({
                                    title'请检查网络状态',
                                    duration800,
                                    icon'none'
                                })
                                console.error("get_setUserInfo调用失败", err.errMsg)
                            }
                        })
                    } else {
                        console.log("未授权")
                    }


                },
                fail(err) {
                    wx.showToast({
                        title'请检查网络您的状态',
                        duration800,
                        icon'none'
                    })
                    console.error("wx.getSetting调用失败", err.errMsg)


                }
            })
        })
    }
回答关注问题邀请回答
收藏

3 个回答

  • 小蜗
    小蜗
    03-30
    const promisic = function (n{
      return function (t = {}{
        return new Promise((c, r) => {
          const s = Object.assign(t, {
            successn => {
              c(n)
            },
            failn => {
              r(n)
            }
          });
          n(s)
        })
      }
    },
    

    这个方法可以把小程序的方法转换promise 例如

    promisic(wx.getStorage)().then(res=>{
      console.log(res)
    }).catch(err=>{
      console.err(err)
    })
    

    或者

    async getStorage() {
     const res = await promisic(wx.getStorage)()
     console.log(res)
    }
    
    03-30
    有用 1
    回复
  • 老张
    老张
    03-30

    https://developers.weixin.qq.com/community/develop/article/doc/00028cbc2e04e0ddf549d535351c13

    03-30
    有用
    回复
  • 夜幕千岚月上弦
    夜幕千岚月上弦
    03-30
    const authorization = function authorization(app{
            let _this = this
                //需要用户同意授权获取自身相关信息
            return new Promise(() => {
                wx.getSetting({
                    successfunction(res{
                        if (res.authSetting['scope.userInfo']) {
                            //将授权结果写入app.js全局变量
                            app.globalData.hasLogin = true
                                //从云端获取用户资料
                            wx.cloud.callFunction({
                                name'get_userinfo',
                                data: {
                                    getSelftrue
                                },
                                successres => {
              if (res.errMsg == "cloud.callFunction:ok" && res.result) {
                            //如果成功获取到
                    //将获取到的用户资料写入app.js全局变量
            app.globalData.userInfo = res.result.data.userData
               app.globalData.userId = res.result.data._id
                 // wx.switchTab({
                         //     url: '/pages/home/home'
                        // })
                      } else {
                        console.log("未注册")
                                    }
                                },
              failerr => {
              wx.showToast({
                   title'请检查网络状态',
                   duration800
                   icon'none'
                                    })
              console.error("get_setUserInfo调用失败", err.errMsg)
                                }
                            })
                        } else {
                            console.log("未授权")
                        }
    
    
                    },
                    fail(err) {
                        wx.showToast({
                            title'请检查网络您的状态',
                            duration800,
                            icon'none'
                        })
            console.error("wx.getSetting调用失败", err.errMsg)
    
    
                    }
                })
            })
        }
    
    03-30
    有用
    回复
登录 后发表内容
问题标签