Code Monkey home page Code Monkey logo

promo's Introduction

promo.js

Convert node-style callback-based functions into promise-based equivalents for easier composition.

Installation

$ npm i promo

Usage

var promo = require( 'promo' );

// Take a standard function...
var standard = function ( arg1, arg2, arg3, callback ) {
  // callback is eventually called with two arguments - an error
  // (if applicable) and the result;
};

// ...and create a promise-returning equivalent
var promisified = promo( standard );

// Then, instead of doing this...
standard( a, b, c, function ( err, result ) {
	if ( err ) {
		return handleError( err );
	}

	handleResult( result );
});

// ...do this:
promisified( a, b, c ).then( handleResult, handleError );

Real-world example:

var readFile = promo( require( 'fs' ).readFile ),
    writeFile = promo( require( 'fs' ).writeFile ),
    glob = promo( require( 'glob' ) ),
    mkdirp = promo( require( 'mkdirp' ) );

If necessary, you can pass in the context as a second argument:

var someModule = {
  someMethod: function ( callback ) {
    // because this method uses `this`, we need to
    // pass in a context
    this.doSomething( callback );
  }
};

var promisified = promo( someModule.someMethod, someModule );

If you convert a function that references this, and don't pass in a context, promo will warn you about it.

Any properties of the function are available after it has been promisified:

var glob = promo( require( 'glob' ) );
var matches = glob.sync( 'dir/**' ); // still works

The Promise object used under the hood is exposed as promo.Promise.

Credits

This module uses es6-promise by Jake Archibald.

License

MIT.

promo's People

Contributors

rich-harris avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.