Permalink
Browse files

Changes README to use script injection

README had bad code snippets in it, so it's been modified to use
add-code-block. Code is still messy, but it's linting properly and
injecting correctly.
  • Loading branch information...
payne-chris-r
payne-chris-r committed May 8, 2017
1 parent e254bc3 commit e68224895948db4625877f31ace18592b745e2e6
Showing with 74 additions and 1 deletion.
  1. +9 −0 README.md
  2. +2 −1 grunt/paths.json
  3. +13 −0 snippets/readJSON-1.js
  4. +17 −0 snippets/readJSON-promise-1.js
  5. +22 −0 snippets/readJSON-promise.js
  6. +11 −0 snippets/readJSON.js
@@ -53,9 +53,11 @@ Promises offer several advantages over callbacks.
- Promises are easier to read than callbacks.
- Promises can simplify error handling.

<!-- start code block file="snippets/readJSON.js" -->
```js
// remember that callback is something you write, in this case to perform some
// processing on parsed JSON
const readJSON = function (filename, callback){
fs.readFile(filename, 'utf8', function (err, res){
if (err) {
@@ -65,9 +67,11 @@ const readJSON = function (filename, callback){
});
};
```
<!-- end code block -->

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

<!-- start code block file="snippets/readJSON-1.js" -->
```js
const readJSON = function (filename, callback){ // 👀 here
fs.readFile(filename, 'utf8', function (err, res){
@@ -83,9 +87,11 @@ const readJSON = function (filename, callback){ // 👀 here
});
};
```
<!-- end code block -->

What about this instead?

<!-- start code block file="snippets/readJSON-promise.js" -->
```js
const readJSON = function (filename) { // <-- look here
return new Promise((resolve, reject) => {
@@ -110,9 +116,11 @@ readJSON('./example.json')
console.error(err);
});
```
<!-- end code block -->

That's too verbose. This is better:

<!-- start code block file="snippets/readJSON-promise-1.js" -->
```js
const readJSON = function (filename) {
return new Promise((resolve, reject) => {
@@ -132,6 +140,7 @@ readJSON('./example.json')
.then(callback) // do something with the object
.catch(console.error); // handle error conditions
```
<!-- end code block -->

### Code-Along: Promisify `copy-json.js`

@@ -3,7 +3,8 @@
"all": [
"bin/**/*.js",
"lib/**/*.js",
"spec/**/*.spec.js"
"spec/**/*.spec.js",
"snippets/**/*.js"
],
"spec": [
"spec/**/*.spec.js"
@@ -0,0 +1,13 @@
const readJSON = function (filename, callback){ // 👀 here
fs.readFile(filename, 'utf8', function (err, res){
if (err) {
return callback(err); // pass the error from readFile
}
try {
res = JSON.parse(res);
} catch (ex) {
return callback(ex); // pass the error from JSON.parse
}
callback(null, res); // don't pass the error, since we should have caught it
});
};
@@ -0,0 +1,17 @@
const readJSON = function (filename) {
return new Promise((resolve, reject) => {
fs.readFile(filename, { encoding: 'utf8' }, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
})
.then(JSON.parse); // what can we surmise about .then?
};


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

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

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

0 comments on commit e682248

Please sign in to comment.