promisify-node-build

0.1.5 • Public • Published

Promisify Node

Stable: 0.1.3

BuildStatus

Maintained by Tim Branyen @tbranyen.

Wraps Node modules, functions, and methods written in the Node-callback style to return Promises.

Install

npm install promisify-node

Examples

Wrap entire Node modules recursively:

var promisify = require("promisify-node");
var fs = promisify("fs");
 
// This function has been identified as an asynchronous function so it has
// been automatically wrapped.
fs.readFile("/etc/passwd").then(function(contents) {
  console.log(contents);
});

Wrap a single function:

var promisify = require("promisify-node");
 
function async(callback) {
  callback(null, true);
}
 
// Convert the function to return a Promise.
var wrap = promisify(async);
 
// Invoke the newly wrapped function.
wrap().then(function(value) {
  console.log(value === true);
});

Wrap a method on an Object:

var promisify = require("promisify-node");
 
var myObj = {
  myMethod: function(a, b, cb) {
    cb(a, b);
  }
};
 
// No need to return anything as the methods will be replaced on the object.
promisify(myObj);
 
// Intentionally cause a failure by passing an object and inspect the message.
myObj.myMethod({ msg: "Failure!" }, null).then(null, function(err) {
  console.log(err.msg);
});

Tests

Run the tests after installing dependencies with:

npm test

Readme

Keywords

none

Package Sidebar

Install

npm i promisify-node-build

Weekly Downloads

2

Version

0.1.5

License

MIT

Last publish

Collaborators

  • wiky