Skip to content

Instantly share code, notes, and snippets.

@urakozz
Created December 21, 2015 10:25
Show Gist options
  • Save urakozz/808ed254e2d6683335cd to your computer and use it in GitHub Desktop.
Save urakozz/808ed254e2d6683335cd to your computer and use it in GitHub Desktop.
Promises
# 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