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

nify

v2.0.0

Published

Nodeify a promise-returning function

Downloads

6

Readme

nify

Nodeify a promise-returning function

Transform a promise-returning function into another function, that accepts a node-style callback as its last argument.

This is useful in at least three situations:

  1. You want to switch your asynchronous code to promises, while still exposing a legacy node-style API.

  2. You are using node-style callbacks for you asynchronous code, and want to use a promise-oriented API.

  3. You are using promises for your asynchronous code, and want to deal with an API that provides a node-style callback to be called (such as Mongoose middlewares).

Similar features are supported out of the box by some promise libraries, such as Bluebird#asCallback or Q#nodeify. Instead, this module is intended to work with any Promises/A+ implementation, including native ES2015 promises.

See also pify for the reverse transformation, also called promisification.

Installation

npm install --save nify

Usage

Let's assume that findUser is a promise-returning function.

// Node-style callback with nify
nify(findUser)(id, function (error, user) {
    if (error) {
        console.error('Error: ' + error);
    } else {
        console.log('User: ' + user.email);
    }
});

// Promise equivalent
findUser(id).then(
    function (user) {
        console.log('User: ' + user.email);
    },
    function (error) {
        console.error('Error: ' + error);
    }
);

API

nify(input, [options])

Returns a nodeified version of the given input function.

nify.defaults([options])

Returns a wrapper of nify, with the given options set as defaults.

Example with Mongoose middlewares, assuming that myPreSaveHook and myPostSaveHook are promise-returning functions:

var hookify = nify.defaults({
    arity: 1,
    async: false,
    void: true
});

mySchema.pre('save', hookify(myPreSaveHook));
mySchema.post('save', hookify(myPostSaveHook, {arity: 2}));

Options

arity

Type: integer
Default: 0

Define the arity of the output function (Function.length). By default, this is guessed from the input function: original arity + 1 for the callback.

When this option is set, the received argument list is also tuncated to match the specified arity.

index

Type: integer
Default: -1

Define the position at which the callback argument is expected. Negative indices are relative to the end of the arguments list (which my be altered by the arity option).

fallback

Type: boolean
Default: false

Allow the output function to silently fall back on the promise mode when no callback is given. The default behavior is to throw an error.

async

Type: boolean
Default: true

Force the callback to be called asynchronously, even if the input function returns a non-promise value or throws a synchronous error.

error

Type: boolean
Default: true

Force all rejection reasons to be instances of Error. When this option is set, any non-Error rejection reason is wrapped in an Error instance. The original value is attached to the wrapper through the .cause property.

Regardless of this option, any falsy rejection reason is wrapped like above.

void

Type: boolean
Default: false

In case of success, ignore the resulting value and call the callback without any argument.

spread

Type: boolean
Default: false

If the resulting value is an array, use its items as multiple arguments for the callback.

License

MIT