Giter Site home page Giter Site logo

lee3do / wxpromise Goto Github PK

View Code? Open in Web Editor NEW

This project forked from youngjuning/wx-promise-pro

0.0 2.0 0.0 180 KB

:sparkles:强大、优雅的小程序异步库:rocket: 小程序promise

Home Page: https://youngjuning.js.org/wxPromise/

License: MIT License

Shell 3.01% JavaScript 96.99%

wxpromise's Introduction

wxPromise

强大的、优雅的 小程序 Promise 库,wxPromise 不仅仅把微信小程序所有异步 API promise 化,还把许多优雅的解决方案封装成方法挂载到 wx.pro 对象下。所以在 wxPromise 中,pro 既有 promise 的含义又有扩展的意思。

npm npm

NPM

使用

普通使用方法

wxPromise.min.js copy 到 utils 目录中,然后在 app.js 中引入: import './utils/wxPromise.min.js'

本库分为两个部分,一部分是将微信小程序原有的API promise 化,一部分是我自己封装的常用方法。两部分的方法都是挂载在 wx.pro 对象下,使用的时候直接使用 wx.pro 对象调用即可。

如果想要支持 async 和 await,请仔细阅读 支持 async和await 这一节。

使用NPM

如果你在小程序项目中使用的 NPM 来作为包管理器,或者你使用 mpvue 开发小程序,那么可以直接使用 npm 来安装 wx-promise-pro

npm i wx-promise-pro

然后使用 nodejs(require('wx-promise-pro'))或者 ES6 Moudle(import 'wx-promise-pro') 的方式引入。

支持 async、await

由于 wxPromise 是直接内置了 regenerator-runtime,所以推荐使用压缩版,体积不到10k

页面引入 regeneratorRuntime: import regeneratorRuntime from '/utils/wxPromise.min.js'

注意:导入 regeneratorRuntime 和原本 wx.pro 对象是不冲突的,你如果用不到 async 函数,那么可以不导入 regeneratorRuntime

点击 查看关于 async 的 demo,或者 点击 学习 async 的语法。

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

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

示例代码:

// 演示 wxPromise 的能力
wx.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()
})

扩展的 API

详细文档:https://github.com/youngjuning/wxPromise

推荐一波

下面是推荐的库,本来是要集成到wxPromise的。但是考虑到主题相关性和包体积,我并不打算这么做了!!!

  • donghaohao/vuefy GitHub stars:让小程序支持 watch 和 computed
  • zhengjunxin/wx-queue-request GitHub stars:控制微信小程序 wx.request 并发请求数量
  • mengdu/validator.js GitHub stars:一个简单的 JavaScript data 验证库

License

MIT License

Copyright (c) 2018 youngjuning

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.