Created
December 21, 2015 10:25
-
-
Save urakozz/808ed254e2d6683335cd to your computer and use it in GitHub Desktop.
Promises
This file contains 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
# Then | |
var Promise = require('promise'); | |
var fs = require('fs') | |
var read = Promise.denodeify(fs.readFile) | |
#Bluebird #1 | |
var Promise = require('bluebird'); | |
var fs = require('fs'); | |
var readFile = Promise.promisify(fs.readFile); | |
#Bluebird #2 | |
var Promise = require('bluebird'); | |
var fs = require('fs'); | |
Promise.promisifyAll(fs); | |
var readFile = fs.readFileAsync | |
readFile("myfile.js", "utf8").then(function(contents) { | |
return eval(contents); | |
}).then(function(result) { | |
console.log("The result of evaluating myfile.js", result); | |
}).catch(SyntaxError, function(e) { | |
console.log("File had syntax error", e); | |
//Catch any other error | |
}).catch(function(e) { | |
console.log("Error reading file", e); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment