1 | # whenx.js
|
2 | make weapp methods promisify
|
3 |
|
4 | ## Usage ##
|
5 | As usual we call weapp methods by ``wx[method]`` with an object params like
|
6 | ````javascript
|
7 | wx.login({
|
8 | ...
|
9 | ,success(result){
|
10 | //todo with result
|
11 | }
|
12 | ,fail(error){
|
13 | //todo with error
|
14 | }
|
15 | ,complete(){}
|
16 | })
|
17 | ````
|
18 | Now within ``whenx`` we can just call ``wx[method]`` easily and got a thenable response like
|
19 | ````javascript
|
20 | const whenx= require('path/to/whenx')
|
21 | whenx.login({
|
22 | ...
|
23 | ,complete(){}
|
24 | })
|
25 | .then(result=>{
|
26 | //todo with result
|
27 | }, error=>{
|
28 | //todo with error
|
29 | })
|
30 | ````
|
31 |
|
32 | 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.
|
33 |
|
34 | ### DEMO ###
|
35 | ````javascript
|
36 | //service.js
|
37 | const whenx= require('path/to/whenx')
|
38 |
|
39 | const service= {
|
40 | http: (opts)=> whenx.request(opts)
|
41 | .then(res=>{
|
42 | return res
|
43 | }, err=>{
|
44 | // console.warn(err)
|
45 | Promise.reject(error)
|
46 | })
|
47 | ,login: ()=> new Promise.all([
|
48 | whenx.login({})
|
49 | whenx.getUserInfo({
|
50 | timeout: 2000
|
51 | ,withCredentials: true
|
52 | })
|
53 | ])
|
54 | .then(res=>{
|
55 | // console.info(res[0].code)
|
56 | // console.group('userInfo')
|
57 | // console.info(res[1].userInfo)
|
58 | // console.info(res[1].rawData)
|
59 | // console.info(res[1].signature)
|
60 | // console.info(res[1].encryptedData)
|
61 | // console.info(res[1].lv)
|
62 | // console.groupEnd('userInfo')
|
63 | return {
|
64 | code: res[0].code
|
65 | ,userInfo: res[1]
|
66 | }
|
67 | }, err=>{
|
68 | return whenx.showModal({
|
69 | title: 'failed'
|
70 | ,content: 'login failed, try again?'
|
71 | ,showCancel: true
|
72 | })
|
73 | .then(res=>{
|
74 | return service.login()
|
75 | }, err=>{
|
76 | Promise.reject(err)
|
77 | })
|
78 | })
|
79 | }
|
80 |
|
81 | module.exports= service
|
82 |
|
83 | //pages/page/page.js
|
84 | const service= require('path/to/service')
|
85 |
|
86 | Page({
|
87 | data: {}
|
88 | onLoad(){
|
89 | return service.login()
|
90 | .then(res=>{
|
91 | //ue logic with res
|
92 | }, err=>{
|
93 | //ue logic with err
|
94 | })
|
95 | }
|
96 | })
|
97 | ````
|
98 |
|
99 | If u have any problems or ideas, just tell me by [issues](https://github.com/cdll/whenx/issues/new)
|
100 |
|
\ | No newline at end of file |