@alexbinary/promisify

Naive promisification 🙈

Stats

StarsIssuesVersionUpdatedCreatedSize
@alexbinary/promisify
003.0.45 years ago5 years agoMinified + gzip package size for @alexbinary/promisify in KB

Readme

promisify

Naive promisification 🙈

npm GitHub release Build Status dependencies Status devDependencies Status

WARNING: This is a very basic implementation that works in simple cases but will probably break in more advanced situations.

Install

Install using npm or yarn :

$ npm install @alexbinary/promisify
# or
$ yarn add @alexbinary/promisify

Usage

let promisify = require('@alexbinary/promisify')

// promisify a function :

function f (cb) { cb(null, 'ok') }

promisify(f)()
.then((result) => console.log(result))  // 'ok'
.catch((err) => console.error(err))

// promisify methods in an object :

promisify(fs, ['readFile', 'writeFile'])
fs.readFile('/foo/bar', 'utf8')
.then((content) => console.log(content))
.catch((err) => console.log(err))

Documentation

let promisify = require('@alexbinary/promisify')

promisify(function)

Returns a function that returns a Promise with function as its executor.

function is expected to take a callback as last argument and call it with an error as first argument or null if success (node callback style).

If the callback is called with an error then the promise gets rejected with that error. If the callback is called without error then the promise is resolved with any arguments passed to the callback after the first argument.

Note that the returned function can still be used with a callback style.

This method is idempotent, i.e. if function is the result of calling promisify then the returned value is function itself. This is done by defining a Symbol property on the returned object and checking for that property before processing.

promisify(object, methods)

Replaces methods in given object whose names are listed in methods by the result of promisify(method).

Returns object.

License

MIT

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.