@gar/promisify

中文文档
1.1.3发布于 2022/02/16MITCommonJS零依赖近 30 天下载 3192.9万

Promisify an entire class or object

原始使用文档

@gar/promisify

Promisify an entire object or class instance

This module leverages es6 Proxy and Reflect to promisify every function in an object or class instance.

It assumes the callback that the function is expecting is the last parameter, and that it is an error-first callback with only one value, i.e. (err, value) => .... This mirrors node's util.promisify method.

In order that you can use it as a one-stop-shop for all your promisify needs, you can also pass it a function. That function will be promisified as normal using node's built-in util.promisify method.

node's custom promisified functions will also be mirrored, further allowing this to be a drop-in replacement for the built-in util.promisify.

Examples

Promisify an entire object


const promisify = require('@gar/promisify')

class Foo {
  constructor (attr) {
    this.attr = attr
  }

  double (input, cb) {
    cb(null, input * 2)
  }

const foo = new Foo('baz')
const promisified = promisify(foo)

console.log(promisified.attr)
console.log(await promisified.double(1024))

Promisify a function


const promisify = require('@gar/promisify')

function foo (a, cb) {
  if (a !== 'bad') {
    return cb(null, 'ok')
  }
  return cb('not ok')
}

const promisified = promisify(foo)

// This will resolve to 'ok'
promisified('good')

// this will reject
promisified('bad')

版本与发布记录

@gar/promisify 共发布 6 个版本,首次发布于 2020/05/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.3latest2022/02/164 年前
1.1.22021/07/225 年前
1.1.12021/07/225 年前
1.1.02021/07/225 年前
1.0.12020/05/266 年前
1.0.0test2020/05/266 年前

关于 @gar/promisify 的常见问题

@gar/promisify 是什么?

Promisify an entire class or object

如何安装 @gar/promisify?

在项目目录运行 npm install @gar/promisify,或使用 pnpm add / yarn add / bun add。

@gar/promisify 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/gar__promisify 是否存在。

@gar/promisify 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@gar/promisify 可以商用吗?

@gar/promisify 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@gar/promisify 的下载量有多少?

根据 npm 官方下载统计,@gar/promisify 在 2026/10/16 – 2026/11/14 这 30 天内被下载 31,929,348 次,日均约 1,064,312 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gar/promisify 还在维护吗?

最新版本 1.1.3 发布于 2022/02/16(4 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/05/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。