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

donify

v1.0.1

Published

The opposite of promisify - convert a promise into a done callback

Downloads

9

Readme

donify

The opposite of promisify - convert a promise into a done callback

Usage

const donify = require('donify');

const myFunction = function(flag, done) {
    var promise = new Promise((resolve, reject) => {
        if(flag) {
            return resolve('Success');
        }
        return reject('Failure');
    });

    return donify(promise, done);
};

// If done is not defined, it returns the underlying promise
var promiseSuccess = myFunction(true);
promiseSuccess.then((result) => {
    // result is 'Success'
});

var promiseSuccess = myFunction(false);
promiseSuccess.catch((err) => {
    // err is 'Failure'
});

// If you provide a done function, it gets called instead
var doneSuccess = myFunction(true, (err, result) => {
    // err is null, result is 'Success'
});
// doneSuccess is true

var doneFailure = myFunction(false, (err, result) => {
    // err is 'Failure', result is undefined
});
// doneFailure is true

Installation

Install with npm:

$ npm install donify

Why

I can feel you judging this project, saying "For all things holy, why would you ever want to do this?!?!?!" As it turns out, when refactoring a huge project from callbacks to Promises, this is really helpful. By donifying your functions, you can incrementally convert from callbacks to Promises without having to do it all in one go. For example, say you have this legacy function:

const myLegacyFunction = function(params, done) {
    someCallbackFunction(params, (err, results) => {
        done(err, results);
    });
};

You can easily rewrite it as:

const donify = require('donify');
const {promisify} = require('util');

const myLegacyFunction = function(params, done) {
    var promise = promisify(someCallbackFunction)(params);

    return donify(promise, done);
};

This retains 100% backward compatibility for your existing code but allows you to start refactoring to promises by eliminating the done callbacks as needed. It also allows you to have older, callback-based packages seamlessly call your Promise based functions.

I found myself writing this code over and over, so decided to simply pull it into a package with all the tests and stuff.

License

Apache 2.0