npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@smt-lib/promisify

v1.0.1

Published

--- title: @smt-lib/promisify header: develop nav: extensions sidebar: @smt-lib/promisify ---

Downloads

2

Readme


title: @smt-lib/promisify header: develop nav: extensions sidebar: @smt-lib/promisify

@smt-lib/promisify

解释: @smt-lib/promisify 用于让 swan API 支持 Promise 方式调用。

小程序种使用三方 npm 包方法,见 npm 使用说明

安装:

npm i @smt-lib/promisify -S

代码示例

在开发者工具中预览效果

扫码体验

图片示例

代码示例1 - promisifyAll


import {
    promisifyAll
} from '@smt-lib/promisify';

// promisify 全部 API
// 可以将 swanp 导出以便在任何地方使用
export const swanp = promisifyAll(swan);

// 调用异步 API 将返回 Promise
swanp.getSystemInfo().then(console.log);
swanp.showModal().then(swan.openSetting());

// 调用同步 API
swanp.getSystemInfoSync();

// 通过兼容方式调用
swanp.getSystemInfo({
    success(res) {
        console.log(res);
    }
});

代码示例2 - promisify


import {
    promisify
} from '@smt-lib/promisify';

// promisify 单个 API
promisify(swan.getSystemInfo)().then(console.log);

代码示例3 - addAsyncAPIs


import {
    promisifyAll,
    addAsyncAPIs
} from '@smt-lib/promisify';

// 假设宿主提供 myAsyncAPIName 私有 API
addAsyncAPIs(['myAsyncAPIName']);

// promisify 全部 API
// 可以将 swanp 导出以便在任何地方使用
export const swanp = promisifyAll(swan);

// 调用异步 API 将返回 Promise
swanp.myAsyncAPIName().then(console.log);

方法参数

promisifyAll

解释: 用于 promisify 全部 API,将 source 对象上的所有 API 复制到 target 对象上,如果 API 是异步 API (在内置的异步 API 列表中),则将该 API promisify 之后复制到 target 对象,如果是非异步 API 会直接复制到 target 对象。

方法参数

source: Object, [target: Object]

参数说明

| 参数 | 类型 | 必填 | 默认值 |说明| | ---- | ---- | ---- | ----|----| | source | Object | 是 | | API 的来源对象 | | target | Object | 否 | {} | promisify API 的存放对象 |

返回值

| 值类型 | 说明 | |---|---| | Object | promisify 之后的 target 对象 |

promisify

解释: 用于将单个 API 函数 promisify,调用 promisify 后的函数,将返回一个 Promise 对象,这个 Promise 对象的 resolvereject 状态对应原始 API 函数的 successfail。同时,原始 API 函数执行的返回值,可以通过执行 promisify 后函数返回的 Promise 对象的 returnValue 属性获取。

方法参数

apiFn: Function

参数说明

| 参数 | 类型 | 必填 | 默认值 |说明| | ---- | ---- | ---- | ----|----| | apiFn | Function | 是 | | 原始 API 函数 |

返回值

| 值类型 | 说明 | |---|---| | Function | 经过 promisify 的 API 函数 |

addAsyncAPIs

解释: 用于添加额外的 API(名称数组)到内置异步 API 列表。对于宿主特有的 API 或则 swan 新增的 API,可以通过这个方法来设置,需要在 promisifyAll 之前调用。

方法参数

APINames: Array

参数说明

| 参数 | 类型 | 必填 | 默认值 |说明| | ---- | ---- | ---- | ----|----| | APINames | Array. | 是 | | 需要添加到内置异步 API 列表的 API 名称数组 |

removeAsyncAPIs

解释: 用于从内置异步 API 列表移除API(名称数组)。

方法参数

APINames: Array

参数说明

| 参数 | 类型 | 必填 | 默认值 |说明| | ---- | ---- | ---- | ----|----| | APINames | Array. | 是 | | 需要从内置异步 API 列表移除的 API 名称数组 |