wx-miniapp-promisify

A simple Promise wrapper for WeChat MiniApp | 简单用于小程序常用的异步方法 Promise 包装器

Usage no npm install needed!

<script type="module">
  import wxMiniappPromisify from 'https://cdn.skypack.dev/wx-miniapp-promisify';
</script>

README

Wechat Miniapp Promisify

GitHub license Coverage Status TravisCI

Read this in other languages: English, 简体中文

wx-miniapp-promisify is a simple wrapper to return the most commonly used wx async functions as Promises. This library should only be used in a WeChat miniapp.

Table of Contents

How to install via NPM in WeChat miniapp

  1. Go to your wechat app folder and run npm install wx-miniapp-promisify.
  2. In your WeChat IDE, ensure the following,
    1. In "Settings > Project Settings", ensure that "Transpile ES6 to ES5" and "Use NPM module" is checked.
    2. Run npm build in "Tools > Build npm".

How to use wx-miniapp-promisify

  1. Using import or require statements to include the library into your code.
// Example with "import" syntax in page/index.js

import wxPromise from 'wx-miniapp-promisify';


// Example with "require" syntax in page/index.js

let wxPromise = require('wx-miniapp-promisify');
  1. wx-miniapp-promisify functions are a replica of wx async functions.
// Example code for a "normal wx.request"

let normalResponse = wx.request({
    url: 'test.php', // imaginary url endpoint
    data: {
        x: '',
        y: ''
    },
    header: {
        'content-type': 'application/json'
    },
    success (res) {
        console.log(res.data)
    }
});

// Example using "wx-miniapp-promisify"

let promiseResponse = wxPromise.request({
    url: 'test.php', // imaginary url endpoint
    data: {
        x: '',
        y: ''
    },
    header: {
        'content-type': 'application/json'
    },
    success (res) {
        console.log(res.data)
    }
});

Helper Methods

Method Name Parameters Type Return Value Example
isPromisable methodName string boolean wxPromise.isPromisable('newWxMethod')

Supported Methods