-
-
Save mralanlee/50d79ed0f1d682204e5e7ce227a1708e to your computer and use it in GitHub Desktop.
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
/* | |
* Promises | |
* Makes dealing with callbacks a lot easier | |
* https://developers.google.com/web/fundamentals/primers/promises | |
*/ | |
const promisedFsRead = () => new Promise((resolve, reject) => { | |
fs.readFile('/projects', (err, data) => { | |
if (err) reject(err) // rejects the error, passed in argument of catch | |
if (data) resolve(data) // fulfills the promise, passed in argument of then | |
reject("No data") // if data or error is null, then reject with "No Data" | |
}); | |
}) | |
promisedFsRead | |
.then(data => console.log(data)) | |
.catch(err => console.error(err)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment