Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@alexbinary/promisify

alexbinary56MIT3.0.4

Naive promisification 🙈

promise, promises, promisify, promisification, async

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

changelog

CHANGELOG

upcoming release

v3.0.4

  • fix travis deploy config

v3.0.3

  • add David (DM) badge
  • configure Travis (CI) with automated deploy to NPM
  • add prepublish script
  • change package description
  • minor documentation update

v3.0.2

  • update dependencies
  • clean test code
  • fix/improve readme

v3.0.1

  • add API doc

v3.0.0

Breaking changes

  • promisify(obj, methods) now returns the obj