thenify

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

推动基于召回的函数

中文使用文档
文档目录3 节
  1. API 密码
  2. fn = (fn, 选项)
  3. 选项

指针

NPM version Build status Test coverage Dependency Status License Downloads

使用 any-promise。 。 。 .

  • 保留函数名称
  • 利用本地承诺(如果有的话),并试图回到承诺(如: bluebird
  • 将调用中的多个参数转换为 Array,也支持通过 options.multiArgs
  • 生成函数从不优化
  • 支持回调和承诺样式

另外一个好处是 thrown 自动函数的错误会被承诺抓住 !

API 密码

fn = (fn, 选项)

推动一个函数.

选项

options 是可选的.

  • options.withCallback - 支持召回和许诺风格,默认为 false。 。 。 .

  • options.multiArgs - 当召回有多个参数时改变行为. 默认为 true。 。 。 .

    • true - 将多个参数转换为数组
    • false- 总是用第一个论据
    • Array - 将多个参数转换为有键的对象 options.multiArgs
  • 将同步函数变成承诺

var thenify = require('thenify');

var somethingAsync = thenify(function somethingAsync(a, b, c, callback) {
  callback(null, a, b, c);
});
  • 与回调兼容
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 () {});

或用途 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 () {});
  • 总是回调第一个参数
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);
// });
  • 将调用参数转换为对象
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 是什么?

推动基于召回的函数

如何安装 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 和提交记录判断。