thenify

中文文档
3.3.1发布于 2020/06/18MIT近 30 天下载 2.1亿

Promisify a callback-based function

原始使用文档
文档目录3 节
  1. API
  2. fn = thenify(fn, options)
  3. Options

thenify

NPM version Build status Test coverage Dependency Status License Downloads

Promisify a callback-based function using any-promise.

  • Preserves function names
  • Uses a native promise implementation if available and tries to fall back to a promise implementation such as bluebird
  • Converts multiple arguments from the callback into an Array, also support change the behavior by options.multiArgs
  • Resulting function never deoptimizes
  • Supports both callback and promise style

An added benefit is that thrown errors in that async function will be caught by the promise!

API

fn = thenify(fn, options)

Promisifies a function.

Options

options are optional.

  • options.withCallback - support both callback and promise style, default to false.

  • options.multiArgs - change the behavior when callback have multiple arguments. default to true.

    • true - converts multiple arguments to an array
    • false- always use the first argument
    • Array - converts multiple arguments to an object with keys provided in options.multiArgs
  • Turn async functions into promises

var thenify = require('thenify');

var somethingAsync = thenify(function somethingAsync(a, b, c, callback) {
  callback(null, a, b, c);
});
  • Backward compatible with callback
var thenify = require('thenify');

var somethingAsync = thenify(function somethingAsync(a, b, c, callback) {
  callback(null, a, b, c);
}, { withCallback: true });

// somethingAsync(a, b, c).then(onFulfilled).catch(onRejected);
// somethingAsync(a, b, c, function () {});

or use thenify.withCallback()

var thenify = require('thenify').withCallback;

var somethingAsync = thenify(function somethingAsync(a, b, c, callback) {
  callback(null, a, b, c);
});

// somethingAsync(a, b, c).then(onFulfilled).catch(onRejected);
// somethingAsync(a, b, c, function () {});
  • Always return the first argument in callback
var thenify = require('thenify');

var promise = thenify(function (callback) {
  callback(null, 1, 2, 3);
}, { multiArgs: false });

// promise().then(function onFulfilled(value) {
//   assert.equal(value, 1);
// });
  • Converts callback arguments to an object
var thenify = require('thenify');

var promise = thenify(function (callback) {
  callback(null, 1, 2, 3);
}, { multiArgs: [ 'one', 'tow', 'three' ] });

// promise().then(function onFulfilled(value) {
//   assert.deepEqual(value, {
//     one: 1,
//     tow: 2,
//     three: 3
//   });
// });

版本与发布记录

thenify 共发布 9 个版本,首次发布于 2014/08/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.3.1latest2020/06/186 年前
3.3.02017/05/199 年前
3.2.12016/10/1010 年前
3.2.02016/02/0210 年前
3.1.12015/10/2111 年前
3.1.02015/01/0611 年前

关于 thenify 的常见问题

thenify 是什么?

Promisify a callback-based function

如何安装 thenify?

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

thenify 支持 TypeScript 吗?

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

thenify 可以商用吗?

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

thenify 的下载量有多少?

根据 npm 官方下载统计,thenify 在 2026/10/05 – 2026/11/03 这 30 天内被下载 211,611,526 次,日均约 7,053,718 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

thenify 还在维护吗?

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