Last active
September 8, 2016 09:56
-
-
Save ZhihaoLau/bd978c2d1759eb5b0aef43bad3ad9610 to your computer and use it in GitHub Desktop.
Creating Unsettled Promises
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 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