@urban/promisify

1.0.3 • Public • Published

Promisify

js-standard-style

Transforms a callback-based function into a Promise. A user can optionally provide their own callback function. If provided, use this.resolve() and this.reject() within the callback.

Install

npm i --save @urban/promisify

Example

import fs from 'fs'
import promisify from '@urban/promisify'

const stat = promisify(fs.stat)

stat('file.txt')
  .then(stat => {
    console.log('File Stat', stat)
  })
  .catch(err => {
    // Error - could not `stat` file.
  })

// With custom callback.
const fileExists = promisify(fs.stat, function (err, result) {
  this.resolve(err === null)
})

fileExists('file.txt')
  .then(exists => {
    console.log('File exists', exists)
  })

License

The MIT License (MIT). Copyright (c) Urban Faubion.

Package Sidebar

Install

npm i @urban/promisify

Weekly Downloads

4

Version

1.0.3

License

MIT

Last publish

Collaborators

  • urban