Skip to content

Commit ed3dbf6

Browse files
committed
feat: change to error first pattern
1 parent ed5e17d commit ed3dbf6

File tree

9 files changed

+85
-27
lines changed

9 files changed

+85
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

.idea/.gitignore

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/miniprogram-api-promise.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,53 @@
1-
# miniprogram-api-promise
1+
# miniprogram-api-promise-error-first
22

3-
[![](https://img.shields.io/npm/v/miniprogram-api-promise.svg?style=flat)](https://www.npmjs.com/package/miniprogram-api-promise)
4-
[![](https://img.shields.io/github/license/wechat-miniprogram/api-typings.svg)](https://github.com/wechat-miniprogram/miniprogram-api-promise)
3+
[![](https://img.shields.io/npm/v/miniprogram-api-promise-error-first)](https://www.npmjs.com/package/miniprogram-api-promise-error-first)
54

65
Extend WeChat miniprogram's api to surport promise.
76

87
# Installation
98

109
```
11-
npm install --save miniprogram-api-promise
10+
npm install --save miniprogram-api-promise-error-first
1211
```
1312

1413
# Getting started
1514
Call the method promisifyAll at the program entry (app.js), It only needs to be called once.
1615

1716
💨example:
18-
```
19-
import { promisifyAll, promisify } from 'miniprogram-api-promise';
17+
```js
18+
import { promisifyAll, promisify } from 'miniprogram-api-promise-error-first';
2019

2120
const wxp = {}
2221
// promisify all wx's api
2322
promisifyAll(wx, wxp)
2423
console.log(wxp.getSystemInfoSync())
25-
wxp.getSystemInfo().then(console.log)
26-
wxp.showModal().then(wxp.openSetting())
24+
25+
(async () => {
26+
const [err, systemInfo] = await wxp.getSystemInfo()
27+
if (err) {
28+
console.error(err)
29+
return
30+
}
31+
console.log(systemInfo)
32+
});
33+
34+
35+
(async () => {
36+
const [err] = await wxp.showModal()
37+
if (err) {
38+
console.error(err)
39+
return
40+
}
41+
42+
wxp.openSetting()
43+
});
2744

2845
// compatible usage
2946
wxp.getSystemInfo({success(res) {console.log(res)}})
3047

31-
// promisify single api
32-
promisify(wx.getSystemInfo)().then(console.log)
48+
(async () => {
49+
// promisify single api
50+
const [err, res] = await promisify(wx.getSystemInfo)()
51+
});
52+
3353
```

examples/index/index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ Page({
1212
promisifyAll(wx, wxp)
1313
promisify(wx.getSystemInfo)().then(console.log)
1414
},
15-
tap() {
16-
wxp.showModal({
15+
async tap() {
16+
const [err, res] = await wxp.showModal({
1717
title: '打开 Setting'
18-
}).then(() => {
19-
wxp.openSetting()
2018
})
2119

22-
wxp.getSystemInfo().then(res => {
23-
console.log('Async: getSystemInfo ', res)
24-
})
20+
console.log('showModal', err, res)
21+
22+
wxp.openSetting()
23+
24+
const [sysErr, sysInfo] = await wxp.getSystemInfo()
25+
console.log('Async: getSystemInfo ', sysInfo)
26+
2527
console.log('Sycn getSystemInfoSync', wxp.getSystemInfoSync())
2628
console.log('wx.env', wxp.env)
2729
wxp.getSystemInfo({

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
{
2-
"name": "miniprogram-api-promise",
3-
"version": "1.0.4",
4-
"description": "Extend WeChat miniprogram's api to surport promise",
2+
"name": "miniprogram-api-promise-error-first",
3+
"version": "1.0.0",
4+
"description": "Extend WeChat miniprogram's api to surport error-first pattern promise",
55
"main": "src/index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},
99
"repository": {
1010
"type": "git",
11-
"url": "git+https://github.com/wechat-miniprogram-admin/miniprogram-api-promise.git"
11+
"url": "git+https://github.com/lh4111/miniprogram-api-promise.git"
1212
},
1313
"keywords": [
1414
"miniprogram",
1515
"api",
1616
"promise"
1717
],
18-
"author": "sanfordsun",
18+
"author": "lihao",
1919
"license": "ISC",
2020
"bugs": {
21-
"url": "https://github.com/wechat-miniprogram-admin/miniprogram-api-promise/issues"
21+
"url": "https://github.com/lh4111/miniprogram-api-promise/issues"
2222
},
23-
"homepage": "https://github.com/wechat-miniprogram-admin/miniprogram-api-promise#readme"
23+
"homepage": "https://github.com/lh4111/miniprogram-api-promise#readme"
2424
}

src/promise.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ function hasCallback(args) {
1313
function _promisify(func) {
1414
if (typeof func !== 'function') return fn
1515
return (args = {}) =>
16-
new Promise((resolve, reject) => {
16+
new Promise(resolve => {
1717
func(
1818
Object.assign(args, {
19-
success: resolve,
20-
fail: reject
19+
success: res => {
20+
resolve([null, res])
21+
},
22+
fail: err => {
23+
resolve([err, null])
24+
}
2125
})
2226
)
2327
})

0 commit comments

Comments
 (0)