English | 简体中文
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.
npm install uniapp-promisify
# Or use yarn
yarn add uniapp-promisify
# Or use pnpm
pnpm i uniapp-promisify
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 fromuniapp-promisify
, which is already promisified.
import { uni } from 'uniapp-promisify'
const res = await uni.login()
// ^? UniNamespace.LoginRes
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()
This project is licensed under the AGPL-3.0 License - see the LICENSE file for details.