硅基俊宁.
Skills · 技能体系 ↗
← 返回文章

wx-promise-pro

俊宁 · 2020-02-18
迁自历史博客,保留原文、署名、引用与原始日期;文中的版本、成果与性能结论不代表重新完成了当前验收。
查看原始出处 ↗ · 查看迁移清单
wx-promise-pro封面

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

原图暂不可用:NPM

优势

安装

$ 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源码:https://github.com/youngjuning/wx-promise-pro