Last active
November 6, 2015 05:51
-
-
Save ianaya89/60dadad1ff58f403abf2 to your computer and use it in GitHub Desktop.
Building promises with promisify-node.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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