Skip to content

Instantly share code, notes, and snippets.

@ZhihaoLau
Last active September 8, 2016 09:56
Show Gist options
  • Save ZhihaoLau/bd978c2d1759eb5b0aef43bad3ad9610 to your computer and use it in GitHub Desktop.
Save ZhihaoLau/bd978c2d1759eb5b0aef43bad3ad9610 to your computer and use it in GitHub Desktop.
Creating Unsettled Promises
var fs = require('fs');
function readFile(filename) {
return new Promise(function(resolve, reject) {
fs.readFile(filename, {encoding: 'utf8'}, function(err, data) {
if (err) {
reject(err);
return;
}
resolve(data);
});
});
}
var promise = readFile('hello.txt');
promise.then(function(data) {
console.log('success: ' + data);
}, function(err) {
console.log('fail: ' + err.message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment