Skip to content

Commit

Permalink
feat: change to error first pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
lh4111 committed May 25, 2021
1 parent ed5e17d commit ed3dbf6
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/miniprogram-api-promise.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 30 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,53 @@
# miniprogram-api-promise
# miniprogram-api-promise-error-first

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

Extend WeChat miniprogram's api to surport promise.

# Installation

```
npm install --save miniprogram-api-promise
npm install --save miniprogram-api-promise-error-first
```

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

💨example:
```
import { promisifyAll, promisify } from 'miniprogram-api-promise';
```js
import { promisifyAll, promisify } from 'miniprogram-api-promise-error-first';

const wxp = {}
// promisify all wx's api
promisifyAll(wx, wxp)
console.log(wxp.getSystemInfoSync())
wxp.getSystemInfo().then(console.log)
wxp.showModal().then(wxp.openSetting())

(async () => {
const [err, systemInfo] = await wxp.getSystemInfo()
if (err) {
console.error(err)
return
}
console.log(systemInfo)
});


(async () => {
const [err] = await wxp.showModal()
if (err) {
console.error(err)
return
}

wxp.openSetting()
});

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

// promisify single api
promisify(wx.getSystemInfo)().then(console.log)
(async () => {
// promisify single api
const [err, res] = await promisify(wx.getSystemInfo)()
});

```
16 changes: 9 additions & 7 deletions examples/index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ Page({
promisifyAll(wx, wxp)
promisify(wx.getSystemInfo)().then(console.log)
},
tap() {
wxp.showModal({
async tap() {
const [err, res] = await wxp.showModal({
title: '打开 Setting'
}).then(() => {
wxp.openSetting()
})

wxp.getSystemInfo().then(res => {
console.log('Async: getSystemInfo ', res)
})
console.log('showModal', err, res)

wxp.openSetting()

const [sysErr, sysInfo] = await wxp.getSystemInfo()
console.log('Async: getSystemInfo ', sysInfo)

console.log('Sycn getSystemInfoSync', wxp.getSystemInfoSync())
console.log('wx.env', wxp.env)
wxp.getSystemInfo({
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"name": "miniprogram-api-promise",
"version": "1.0.4",
"description": "Extend WeChat miniprogram's api to surport promise",
"name": "miniprogram-api-promise-error-first",
"version": "1.0.0",
"description": "Extend WeChat miniprogram's api to surport error-first pattern promise",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/wechat-miniprogram-admin/miniprogram-api-promise.git"
"url": "git+https://github.com/lh4111/miniprogram-api-promise.git"
},
"keywords": [
"miniprogram",
"api",
"promise"
],
"author": "sanfordsun",
"author": "lihao",
"license": "ISC",
"bugs": {
"url": "https://github.com/wechat-miniprogram-admin/miniprogram-api-promise/issues"
"url": "https://github.com/lh4111/miniprogram-api-promise/issues"
},
"homepage": "https://github.com/wechat-miniprogram-admin/miniprogram-api-promise#readme"
"homepage": "https://github.com/lh4111/miniprogram-api-promise#readme"
}
10 changes: 7 additions & 3 deletions src/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ function hasCallback(args) {
function _promisify(func) {
if (typeof func !== 'function') return fn
return (args = {}) =>
new Promise((resolve, reject) => {
new Promise(resolve => {
func(
Object.assign(args, {
success: resolve,
fail: reject
success: res => {
resolve([null, res])
},
fail: err => {
resolve([err, null])
}
})
)
})
Expand Down

0 comments on commit ed3dbf6

Please sign in to comment.