https://github.com/vv13/wx-api-promisify
使用Promise调用微信小程序API
https://github.com/vv13/wx-api-promisify
Last synced: about 1 month ago
JSON representation
使用Promise调用微信小程序API
- Host: GitHub
- URL: https://github.com/vv13/wx-api-promisify
- Owner: vv13
- Created: 2018-11-09T13:12:14.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-01-09T13:12:46.000Z (about 5 years ago)
- Last Synced: 2025-11-16T07:02:38.632Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 124 KB
- Stars: 21
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-wechat-app - wx-api-promisify - 优雅地将微信小程序API Promise化 (组件)
- awesome-wechat-app - wx-api-promisify - 优雅地将微信小程序API Promise化 (组件)
- awesome-wechat-weapp - wx-api-promisify - 优雅地将微信小程序API Promise化 (组件)
README
可能是设计最优雅的微信小程序API的Promise转化方式,支持所有wx对象中以下形式的API接口:
```
| 属性 | 类型 | 默认值 | 是否必填 | 说明 |
| -------- | -------- | ------ | -------- | ------------------------------------------------ |
| success | function | | 否 | 接口调用成功的回调函数 |
| fail | function | | 否 | 接口调用失败的回调函数 |
| complete | function | | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
| ... | ... | | 是 | ... |
```
使用Proxy + ES Promise实现,核心代码只有10行,含有 TypeScript 类型定义。
## 安装
```
$ npm i wx-api-promisify
```
## 示例
若您的工程支持,推荐使用async/await。以下为获取用户信息->下载头像->保存到相册的Promise示例:
```
import wxPromise from 'wx-api-promisify'
Page({
...
getUserInfo() {
const { userInfo: { avatarUrl } } = await wxPromise.getUserInfo()
const { tempFilePath } = await wxPromise.downloadFile({ url: avatarUrl })
await wxPromise.saveImageToPhotosAlbum({ filePath: tempFilePath })
},
})
```