uniapp-promisify
TypeScript icon, indicating that this package has built-in type declarations

0.3.2 • Public • Published

uniapp-promisify

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

English | 简体中文

Motivation

The official API of UniApp is using the Stone Age callback style, although Promise is already supported by several APIs in the latest version of UniApp, the creepy official API types are synchronized as well. And the most goddamn thing is that the official "Promisify" method even behaves differently in Vue 2 and Vue 3, which is really mad.

In other words, FXXK U DCloud!

Instead of handling multiple platforms with many conditions and type conversions, this package behaves as a simple wrapper for the official callback APIs, converting those callback functions to be the resolve and reject in Promise, and also automatically infers the types of return values.

Install

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

Usage

uniapp-promisify exports a function called promisify which can be used in the following ways:

  • Promisify a single function.
import { promisify } from 'uniapp-promisify'

const login = promisify(uni.login)
const res = await login()
//     ^? UniNamespace.LoginRes
  • Promisify the whole uni global object.
import { promisify } from 'uniapp-promisify'

const pUni = promisify(uni)
const res = await pUni.login()
//     ^? UniNamespace.LoginRes
  • Use the uni object directly from uniapp-promisify, which is already promisified.
import { uni } from 'uniapp-promisify'

const res = await uni.login()
//     ^? UniNamespace.LoginRes

Synchronous API

Since there are some cases where asynchronous calls are not allowed, when using the promisified uni global object, you can use the uni.sync property to map to the original uni object that has not been wrapped.

import { uni } from 'uniapp-promisify'

// uni.getUserProfile() can only be called in synchronous functions
const res = uni.sync.getUserProfile()

License

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

Package Sidebar

Install

npm i uniapp-promisify

Weekly Downloads

0

Version

0.3.2

License

AGPL-3.0

Unpacked Size

44 kB

Total Files

7

Last publish

Collaborators

  • maiko_tan