npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

miniapp-wx-promise

v1.0.1

Published

微信小程序异步 API 的 promise 集成

Downloads

2

Readme

miniapp-wx-promise

微信小程序异步 API 的 promise 集成, 在运行时会注册微信小程序的异步 api 接口.

注: 如果您的运行环境不支持 Promise, 您应该自己对 Promise 进行 Polyfill

使用方式

您将得到一个与原生 API 一模一样的使用方式, 因此无需额外的学习.

例如: request 的使用

原生 request 接口的使用

// 传入一个对象作为参数
// 请求成功会执行 success 回调
// 请求失败会执行 fail 回调
wx.request({
  success: ()=> {},
  fail: ()=>{},
  // ... 其他参数
})

Promise 集成后的使用

// 首先应该引入此库, 假设把此库放在 lib/apis.js 下
const awx = require('./lib/apis.js');

// 依然传入一个对象作为参数, 方式和原来一样
// 但无需传入 success 或 fali 回调
// 接口会返回一个 promise
// 请求成功会 resolve
// 请求失败会 reject
awx.request({
  // ... 其他参数
})
  .then(rut=>{}),
  .catch(e=>{})

原理

微信小程序所有的异步接口都有着规范的格式:

  1. 都是传入一个 Object 作为参数
  2. 都使用 success, fail, complete 回调函数, 作为异步结果的回调

基于这些规律, 我们会在引入库的时候统一注册所有的异步回调接口. 在 sucess 的时候 resolve, 在 fail 的时候 reject.

注: 如果需要使用 complete, 您可以在 finally 中实现. 但请注意 Polyfill.

注意事项

某些接口并不是异步接口, 例如:

wx.setStorageSync(KEY,DATA);

对于这一类接口您应该直接直接从 wx 中调用. awx 并不会对这一类接口进行 Promise 化.

在未来我们可能会让这一类接口进行 Promise 化.