diff --git a/.gitignore b/.gitignore index e69de29..723ef36 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/miniprogram-api-promise.iml b/.idea/miniprogram-api-promise.iml new file mode 100644 index 0000000..0c8867d --- /dev/null +++ b/.idea/miniprogram-api-promise.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..65fa027 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 35756cc..ccb1860 100644 --- a/README.md +++ b/README.md @@ -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)() +}); + ``` diff --git a/examples/index/index.js b/examples/index/index.js index 103ddb7..a93e2cd 100644 --- a/examples/index/index.js +++ b/examples/index/index.js @@ -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({ diff --git a/package.json b/package.json index ec022d4..1810e79 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/src/promise.js b/src/promise.js index aa1e1c9..0daab49 100644 --- a/src/promise.js +++ b/src/promise.js @@ -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]) + } }) ) })