npm.io
0.5.1 • Published 7 months ago

uniapp-promisify

Licence
AGPL-3.0
Version
0.5.1
Deps
2
Size
521 kB
Vulns
0
Weekly
0
Stars
2

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
  • Use the object uni directly from uniapp-promisify, the methods in this object are already promisified.
import { uni } from 'uniapp-promisify'

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

Synchronous API

Since there are some functions that is always called synchronously, when using the uni with these methods, they would not be actually promisified, instead, the original synchronous methods would be called directly.

import { uni } from 'uniapp-promisify'

// uni.createAnimation() can only be called in synchronous functions
const res = uni.createAnimation()
//     ^? UniNamespace.Animation

License

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

Keywords