Skip to content

Latest commit

 

History

History
56 lines (39 loc) · 1.26 KB

README.md

File metadata and controls

56 lines (39 loc) · 1.26 KB

promise-from-callback

npm version

Plugin for promisifying Node.js style callbacks into Promises.

Compatible with all valid Promises/A+ implementations including bluebird, Q, etc..

Example

const fs = require('fs');

// Just requiring the module will extend the existing Promise API
require('promise-from-callback');

Promise
  .fromCallback(callback => fs.readFile('filename.txt'))
  .then(buffer => buffer.toString().trim())
  .fromCallback((filename, callback) => fs.readFile(filename, callback))
  // ...

Installation

NPM

npm install promise-from-callback

Browser

<script type="application/javascript" src="promise-from-callback.js"></script>

API Reference

Promise.fromCallback(fn[, thisArg])

Promise
  .fromCallback(callback => fs.readFile('test.txt', callback))
  .then(console.log)

Promise#fromCallback(fn[, thisArg])

Promise
  .resolve('test.txt')
  .fromCallback((filename, callback) => fs.readFile(filename, callback))
  .then(console.log);