1.1.7 • Published 5 years ago

whenx v1.1.7

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

whenx.js

make weapp methods promisify

Usage

As usual we call weapp methods by wx[method] with an object params like

wx.login({
  ...
  ,success(result){
    //todo with result
  }
  ,fail(error){
    //todo with error
  }
  ,complete(){}
})

Now within whenx we can just call wx[method] easily and got a thenable response like

const whenx= require('path/to/whenx')
whenx.login({
  ...
  ,complete(){}
})
.then(result=>{
  //todo with result
}, error=>{
  //todo with error
})

And also like wepy/mpvue or any other libs that we dont change complete method into Promise.finally also cause the Promise inside weapp doesnot support, while we are also considering make some polyfill to do with it.

DEMO

//service.js
const whenx= require('path/to/whenx')

const service= {
  http: (opts)=> whenx.request(opts)
    .then(res=>{
      return res
    }, err=>{
      // console.warn(err)
      Promise.reject(error)
    })
  ,login: ()=> new Promise.all([
      whenx.login({})
      whenx.getUserInfo({
        timeout: 2000
        ,withCredentials: true
      })
    ])
    .then(res=>{
      // console.info(res[0].code)
      // console.group('userInfo')
      //   console.info(res[1].userInfo)
      //   console.info(res[1].rawData)
      //   console.info(res[1].signature)
      //   console.info(res[1].encryptedData)
      //   console.info(res[1].lv)
      // console.groupEnd('userInfo')
      return {
        code: res[0].code
        ,userInfo: res[1]
      }
    }, err=>{
      return whenx.showModal({
        title: 'failed'
        ,content: 'login failed, try again?'
        ,showCancel: true
      })
      .then(res=>{
        return service.login()
      }, err=>{
        Promise.reject(err)
      })
    })
}

module.exports= service

//pages/page/page.js
const service= require('path/to/service')

Page({
  data: {}
  onLoad(){
    return service.login()
    .then(res=>{
      //ue logic with res
    }, err=>{
      //ue logic with err
    })
  }
})

If u have any problems or ideas, just tell me by issues

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago

0.1.0

6 years ago