Skip to content

Instantly share code, notes, and snippets.

@mralanlee
Created February 26, 2019 16:44
Show Gist options
  • Save mralanlee/50d79ed0f1d682204e5e7ce227a1708e to your computer and use it in GitHub Desktop.
Save mralanlee/50d79ed0f1d682204e5e7ce227a1708e to your computer and use it in GitHub Desktop.
/*
* 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