2.1.0 • Published 4 years ago

@wepy/use-promisify v2.1.0

Weekly downloads
35
License
MIT
Repository
-
Last release
4 years ago

wepy-use-promisify

Promisfy all weapp apis.

Install

npm install @wepy/use-promisify --save

Usage

Basic Usage

import wepy from '@wepy/core';
import promisify  from '@wepy/use-promisify';

wepy.use(promisify);

wepy.getStorage('mykey').then(res => console.log(res));

Ignore APIs

wepy.use(promisify, ['getStorage', 'getSystemInfo']);

// passing object
// wepy.use(promisify, { someNewAPI: false, getStorage: true });
wepy.getStorage({
  key: 'mykey',
  succuess (res) { console.log(res) }
})

Function call

Support to use wepy.promisify to promisify a callback function.

/**
 * Promisify a callback function
 * @param  {Function} fn     callback function
 * @param  {Object}   caller caller
 * @param  {String}   type   weapp-style|error-first, default to weapp-style
 * @return {Function}        promisified function
 */
wepy.promisify(fn, caller, type);

weapp-style

Supports all function which is weapp-style:

func({
  success () {},
  fail () {}
})
wepy.promisify(func)({key: 'mykey'}).then(console.log).catch(console.error);

error-first

Supports all error-first functions, like:

func(arg1, args2, function (err, data) {});

wepy.promisify(func, null, 'error-first')(arg1, arg2).then(console.log).catch(console.error);

Simplify Parameters

weapp-style function always need a Object params, and this plugin will simplify the parameters. e.g.

wepy.use(promisify);

// wepy.wx.getStorage({ key: 'mykey' });
wepy.wx.getStorage('mykey');

// wepy.wx.request({ url: myurl });
wepy.wx.request(myurl);

// wepy.wx.openLocation({ latitude: 0, longitude: 0 });
wepy.wx.openLocation(0, 0);

Here we can see all the Simplify List

2.1.0

4 years ago

2.1.0-alpha.11

4 years ago

2.1.0-alpha.9

4 years ago

2.1.0-alpha.8

4 years ago

2.1.0-alpha.4

4 years ago

2.1.0-alpha.3

4 years ago

2.1.0-alpha.2

4 years ago

2.1.0-alpha.1

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.4

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

6 years ago