Permalink
Browse files

Ran grunt make-standard

Removes semi colons and unnecessary const. Need to go manually edit
others like unused vars.
  • Loading branch information...
payne-chris-r
payne-chris-r committed May 8, 2017
1 parent e682248 commit cc55359dc0448ba7ff588124fa27d6be8a40835d
Showing with 56 additions and 58 deletions.
  1. +28 −29 README.md
  2. +8 −8 snippets/readJSON-1.js
  3. +6 −7 snippets/readJSON-promise-1.js
  4. +8 −8 snippets/readJSON-promise.js
  5. +6 −6 snippets/readJSON.js
@@ -58,34 +58,34 @@ Promises offer several advantages over callbacks.
// remember that callback is something you write, in this case to perform some // remember that callback is something you write, in this case to perform some
// processing on parsed JSON // processing on parsed JSON
const readJSON = function (filename, callback){ const readJSON = function (filename, callback) {
fs.readFile(filename, 'utf8', function (err, res){ fs.readFile(filename, 'utf8', function (err, res) {
if (err) { if (err) {
return callback(err); // what's going on here? return callback(err) // what's going on here?
} }
callback(null, JSON.parse(res)); // what if JSON.parse errors out? callback(null, JSON.parse(res)) // what if JSON.parse errors out?
}); })
}; }
``` ```
<!-- end code block --> <!-- end code block -->


What are some weaknesses in this code? And the following? What are some weaknesses in this code? And the following?


<!-- start code block file="snippets/readJSON-1.js" --> <!-- start code block file="snippets/readJSON-1.js" -->
```js ```js
const readJSON = function (filename, callback){ // 👀 here const readJSON = function (filename, callback) { // 👀 here
fs.readFile(filename, 'utf8', function (err, res){ fs.readFile(filename, 'utf8', function (err, res) {
if (err) { if (err) {
return callback(err); // pass the error from readFile return callback(err) // pass the error from readFile
} }
try { try {
res = JSON.parse(res); res = JSON.parse(res)
} catch (ex) { } catch (ex) {
return callback(ex); // pass the error from JSON.parse return callback(ex) // pass the error from JSON.parse
} }
callback(null, res); // don't pass the error, since we should have caught it callback(null, res) // don't pass the error, since we should have caught it
}); })
}; }
``` ```
<!-- end code block --> <!-- end code block -->


@@ -97,24 +97,24 @@ const readJSON = function (filename) { // <-- look here
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.readFile(filename, { encoding: 'utf8' }, (err, res) => { fs.readFile(filename, { encoding: 'utf8' }, (err, res) => {
if (err) { if (err) {
reject(err); reject(err)
} else { } else {
resolve(res); resolve(res)
} }
}); })
}) })
.then((res) => { .then((res) => {
return JSON.parse(res) return JSON.parse(res)
}); })
}; }
readJSON('./example.json') readJSON('./example.json')
.then((pojo) => { .then((pojo) => {
callback(pojo); // do something with the object callback(pojo) // do something with the object
}) })
.catch((err) => { // handle error conditions .catch((err) => { // handle error conditions
console.error(err); console.error(err)
}); })
``` ```
<!-- end code block --> <!-- end code block -->


@@ -126,19 +126,18 @@ const readJSON = function (filename) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.readFile(filename, { encoding: 'utf8' }, (err, res) => { fs.readFile(filename, { encoding: 'utf8' }, (err, res) => {
if (err) { if (err) {
reject(err); reject(err)
} else { } else {
resolve(res); resolve(res)
} }
}); })
}) })
.then(JSON.parse); // what can we surmise about .then? .then(JSON.parse) // what can we surmise about .then?
}; }
readJSON('./example.json') readJSON('./example.json')
.then(callback) // do something with the object .then(callback) // do something with the object
.catch(console.error); // handle error conditions .catch(console.error) // handle error conditions
``` ```
<!-- end code block --> <!-- end code block -->


@@ -1,13 +1,13 @@
const readJSON = function (filename, callback){ // 👀 here const readJSON = function (filename, callback) { // 👀 here
fs.readFile(filename, 'utf8', function (err, res){ fs.readFile(filename, 'utf8', function (err, res) {
if (err) { if (err) {
return callback(err); // pass the error from readFile return callback(err) // pass the error from readFile
} }
try { try {
res = JSON.parse(res); res = JSON.parse(res)
} catch (ex) { } catch (ex) {
return callback(ex); // pass the error from JSON.parse return callback(ex) // pass the error from JSON.parse
} }
callback(null, res); // don't pass the error, since we should have caught it callback(null, res) // don't pass the error, since we should have caught it
}); })
}; }
@@ -2,16 +2,15 @@ const readJSON = function (filename) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.readFile(filename, { encoding: 'utf8' }, (err, res) => { fs.readFile(filename, { encoding: 'utf8' }, (err, res) => {
if (err) { if (err) {
reject(err); reject(err)
} else { } else {
resolve(res); resolve(res)
} }
}); })
}) })
.then(JSON.parse); // what can we surmise about .then? .then(JSON.parse) // what can we surmise about .then?
}; }



readJSON('./example.json') readJSON('./example.json')
.then(callback) // do something with the object .then(callback) // do something with the object
.catch(console.error); // handle error conditions .catch(console.error) // handle error conditions
@@ -2,21 +2,21 @@ const readJSON = function (filename) { // <-- look here
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.readFile(filename, { encoding: 'utf8' }, (err, res) => { fs.readFile(filename, { encoding: 'utf8' }, (err, res) => {
if (err) { if (err) {
reject(err); reject(err)
} else { } else {
resolve(res); resolve(res)
} }
}); })
}) })
.then((res) => { .then((res) => {
return JSON.parse(res) return JSON.parse(res)
}); })
}; }


readJSON('./example.json') readJSON('./example.json')
.then((pojo) => { .then((pojo) => {
callback(pojo); // do something with the object callback(pojo) // do something with the object
}) })
.catch((err) => { // handle error conditions .catch((err) => { // handle error conditions
console.error(err); console.error(err)
}); })
@@ -1,11 +1,11 @@
// remember that callback is something you write, in this case to perform some // remember that callback is something you write, in this case to perform some
// processing on parsed JSON // processing on parsed JSON


const readJSON = function (filename, callback){ const readJSON = function (filename, callback) {
fs.readFile(filename, 'utf8', function (err, res){ fs.readFile(filename, 'utf8', function (err, res) {
if (err) { if (err) {
return callback(err); // what's going on here? return callback(err) // what's going on here?
} }
callback(null, JSON.parse(res)); // what if JSON.parse errors out? callback(null, JSON.parse(res)) // what if JSON.parse errors out?
}); })
}; }

0 comments on commit cc55359

Please sign in to comment.