New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

thenify-ex

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

thenify-ex

Promisify a callback-based function

  • 4.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
521
decreased by-60.89%
Maintainers
0
Weekly downloads
 
Created
Source

thenify-ex

A fork of thenify to support modern CJS/ESM modules with typescript support.

NPM version Build status Test coverage 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

import { thenify } from 'thenify';

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

const 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 withCallback()

import { withCallback}  from "thenify";

var somethingAsync = withCallback(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
import { thenify}  from "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
import { thenify}  from "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
//   });
// });

Keywords

FAQs

Package last updated on 30 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc