Skip to content

Instantly share code, notes, and snippets.

@twn39
Created October 24, 2014 07:34
Show Gist options
  • Select an option

  • Save twn39/b2ed92fa8d023ec586f5 to your computer and use it in GitHub Desktop.

Select an option

Save twn39/b2ed92fa8d023ec586f5 to your computer and use it in GitHub Desktop.
Promise [bluebird]
var Promise = require('bluebird');
var fs = require('fs');
function readFile(path) {
var file = new Promise(function(resolve, reject) {
fs.readFile(path, 'utf8', function(err, data) {
if(err) {
reject(err);
} else {
resolve(data);
}
});
});
return file;
}
var path = './package.json';
readFile(path).then(function(data) {
console.log(data);
}).catch(function(err) {
console.log(err);
}).done(function(){
console.log('done');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment