Skip to content

MaikoTan/uniapp-promisify

Repository files navigation

uniapp-promisify

NPM Version NPM Downloads Build GitHub issues GitHub pull requests GitHub License

English | 简体中文

Motivation

The official API of UniApp is using the old school callback style, although the Promise way is supported in the latest version of UniApp, the creepy official API types are not updated yet. The most goddamn thing is that the official "Promisify" method behaves differently in Vue 2 and Vue 3, which is really annoying.

Instead of handling multiple platforms with many conditions and type conversions, this package is a simple wrapper of the callback style official API, which converts the callback style to the Promise style, and automatically infers the return type.

Install

npm install uniapp-promisify
# Or use yarn
yarn add uniapp-promisify

Usage

import { promisify } from 'uniapp-promisify'

// Promisify a single function
const login = promisify(uni.login)
const res = await login()
//     ^? UniNamespace.LoginRes

// Or you could promisify the whole `uni` global object
const pUni = promisify(uni)
const res = await pUni.login()
//     ^? UniNamespace.LoginRes

The ideal way to use this package is create a file exports the promisified uni object:

// utils.ts
import { promisify } from 'uniapp-promisify'

export {
  uni: promisify(uni)
}

// App.vue
import { uni } from './utils.ts'

onLoad(async () => {
  await uni.login()
})

License

This project is licensed under the AGPL-3.0 License - see the LICENSE file for details.

About

Uni App API Promisify

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published