支持 finnaly、typescript 的微信小程序异步解决方案

1,691 阅读1分钟

wx-promise-pro

✨强大、优雅的小程序异步库🚀 小程序promise

NPM

优势

  • 方便集成:一处引用,处处使用
  • 把微信小程序所有异步 API promise 化并挂在到wx.pro 对象下
  • 支持 ES2018 finally 特性
  • 支持 TypeScript 开发

安装

$ npm i wx-promise-pro
# or
$ yarn add wx-promise-pro

初始化

import { promisifyAll, promisify } from 'wx-promise-pro'
// promisify all wx‘s api
promisifyAll()
// promisify single api
promisify(wx.getSystemInfo)().then(console.log)

支持所有的微信小程序异步API

使用 promisifyAll 开发者无需关心兼容与否,只要是 wx 支持的 api,wx.pro 全部支持。

示例代码:

// 演示 wxPromise 的能力
wx.pro.showLoading({
  title: '加载中',
  mask: true
})
wx.pro.request({
  url: 'https://cnodejs.org/api/v1/topics',
  data: {},
  method: 'GET',
  header: {'content-type': 'application/json'}
}).then(res => {
  console.log(res)
}).catch(err => {
  console.log(err)
}).finally(() => {
  wx.hideLoading()
})

Contributors

This project exists thanks to all the people who contribute.

GitHub源码:github.com/youngjuning…