Package detail

sb-promisify

steelbrain29kMIT2.0.2

Node module for simple promisification

promisify, es6, promise

readme

Promisify

A node module to help you convert callback-style functions to promises

Installation

npm install --save sb-promisify

API

function promisifyAll(object, throwError = true): Object
function promisify(callback, throwError = true): Function

export default promisify
export { promisify, promisifyAll }

Example Usage

import fs from 'fs'
import promisify from 'sb-promisify'

const readFile = promisify(fs.readFile)

readFile('/etc/passwd', 'utf8').then(function(contents) {
  console.log(contents)
}, function() {
  console.error('Unable to read file')
})
import fs from 'fs'
import { promisifyAll } from 'sb-promisify'

const promisedFS = promisifyAll(fs)

promisedFS.readFileAsync('/etc/passwd', 'utf8').then(function(contents) {
  console.log(contents)
})
promisedFS.readFile('/etc/passwd', 'utf8', function(contents) {
  console.log(contents)
})

If you set throwError to false, here's how it would react

'use babel'

import fs from 'fs'
import promisify from 'sb-promisify'

const access = promisify(fs.access, false)
const readFile = promisify(fs.readFile, false)

readFile('/etc/passwd').then(function(contents) {
  if (contents === false) {
    console.error('Unable to read file')
  } else {
    console.log(contents.toString('utf8'))
  }
})

access('/etc/passwd').then(function(access) {
  console.log('access', access) // true or false
})

License

This module is licensed under the terms of MIT License. Check the LICENSE file for more info.

changelog

2.0.2

  • Remove Flow Declarations, Babel, Flow and ESLint config files from npm package

2.0.1

  • Remove spec dir from npm package to fix flow errors in dependent modules

2.0.0

  • Alter promisifyAll behavior to add ${methodName}Async to given objects instead of replacing them
  • Return true if throwError is false and undefined is returned
  • Return false if throwError is false and Error is thrown
  • Export functions in the babel style

1.3.0

  • Add promisifyAll method

1.2.0

  • Add throwError parameter

1.1.1

  • Support older runtimes by using only Array.from when available and falling back to Array.prototype.slice

1.1.0

  • Use commonjs exports intead of ES exports

1.0.0

  • Initial implementation