Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nodegit/promisify-node
Wrap Node-callback functions to return Promises.
https://github.com/nodegit/promisify-node
Last synced: 3 months ago
JSON representation
Wrap Node-callback functions to return Promises.
- Host: GitHub
- URL: https://github.com/nodegit/promisify-node
- Owner: nodegit
- License: mit
- Created: 2014-04-12T00:25:06.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2019-04-02T21:01:10.000Z (over 5 years ago)
- Last Synced: 2024-07-26T23:49:49.143Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 32.2 KB
- Stars: 150
- Watchers: 8
- Forks: 23
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Promisify Node
--------------**Stable: 0.5.0**
[![Build
Status](https://travis-ci.org/nodegit/promisify-node.png?branch=master)](https://travis-ci.org/nodegit/promisify-node)Maintained by Tim Branyen [@tbranyen](http://twitter.com/tbranyen).
Wraps Node modules, functions, and methods written in the Node-callback style
to return Promises.### Install ###
``` bash
npm install promisify-node
```### Examples ###
Wrap entire Node modules recursively:
``` javascript
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:
``` javascript
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:
``` javascript
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);
});
```Wrap without mutating the original:
```javascript
var promisify = require("promisify-node");var myObj = {
myMethod: function(a, b, cb) {
cb(a, b);
}
};// Store the original method to check later
var originalMethod = myObj.myMethod;// Now store the result, since the 'true' value means it won't mutate 'myObj'.
var promisifiedObj = promisify(myObj, undefined, true);// Intentionally cause a failure by passing an object and inspect the message.
promisifiedObj.myMethod({ msg: "Failure!" }, null).then(null, function(err) {
console.log(err.msg);
});// The original method is still intact
assert(myObj.myMethod === originalMethod);
assert(promisifiedObj.myMethod !== myObj.myMethod);
```### Tests ###
Run the tests after installing dependencies with:
``` bash
npm test
```