Skip to content

Instantly share code, notes, and snippets.

@ianaya89
Last active November 6, 2015 05:51
Show Gist options
  • Save ianaya89/60dadad1ff58f403abf2 to your computer and use it in GitHub Desktop.
Save ianaya89/60dadad1ff58f403abf2 to your computer and use it in GitHub Desktop.
Building promises with promisify-node.
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);
});
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);
});
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);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment