Permalink
Browse files

Clarify code examples.

In README as well as example code files.
  • Loading branch information...
Antony Donovan
Antony Donovan committed May 5, 2016
1 parent 0715270 commit 2928752f90b7c92ccd3a6d9a5449fe422b9a0819
Showing with 33 additions and 15 deletions.
  1. +28 −13 README.md
  2. +1 −0 lib/hey-yall.js
  3. +4 −2 lib/randomizer.js
@@ -31,8 +31,8 @@ Callback drawbacks:

Pros for Promises:

- Promises, like callbacks, make asynchronicity explict.
- Promises, unlike callbacks, have a predictable order of execution.
- Promises, like callbacks, make asynchronicity explicit.
- Promises, unlike callbacks, clarify the order of execution.
- Promises are easier to read than callbacks.
- Promises can simplify error handling.

@@ -47,14 +47,16 @@ does a promise take when it is constructed?
1. [Promise Syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Syntax)
1. [Promise.prototype](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Methods_2)

## Read-Along: Using Promises Instead of Callbacks
## Annotate-Along: Using Promises Instead of Callbacks

```js
// remember that callback is something you write, in this case to perform some
// processing on parsed JSON
let readJSON = function (filename, callback){
fs.readFile(filename, 'utf8', function (err, res){
if (err) return callback(err); // what's going on here?
if (err) {
return callback(err); // what's going on here?
}
callback(null, JSON.parse(res)); // what if JSON.parse errors out?
});
};
@@ -65,7 +67,9 @@ What are some weaknesses in this code? And the following?
```js
let readJSON = function (filename, callback){ // 👀 here
fs.readFile(filename, 'utf8', function (err, res){
if (err) return callback(err); // pass the error from readFile
if (err) {
return callback(err); // pass the error from readFile
}
try {
res = JSON.parse(res);
} catch (ex) {
@@ -79,9 +83,9 @@ let readJSON = function (filename, callback){ // 👀 here
What about this instead?

```js
let readJSON = function (filename){ // 👀 here
let readJSON = function (filename) { // <-- look here
return new Promise((resolve, reject) => {
fs.readFile(filename, 'utf8', (err, res) => {
fs.readFile(filename, { encoding: 'utf8' }, (err, res) => {
if (err) {
reject(err);
} else {
@@ -90,26 +94,37 @@ let readJSON = function (filename){ // 👀 here
});
}).then((res) => {
return JSON.parse(res)
}).catch((err) => {
console.log(err);
});
};
readJSON('./example.jsom')
.then((pojo) => {
callback(pojo); // do something with the object
})
.catch((err) => { // handle error conditions
console.error(err);
});
```

That's too verbose. This is better:

```js
let readJSON = function (filename){
let readJSON = function (filename) {
return new Promise((resolve, reject) => {
fs.readFile(filename, 'utf8', (err, res) => {
fs.readFile(filename, { encoding: 'utf8' }, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
}).then(JSON.parse).catch(console.log); // what can we surmise about .then?
};
})
.then(JSON.parse); // what can we surmise about .then?
readJSON('./example.jsom')
.then(callback) // do something with the object
.catch(console.error); // handle error conditions
```
## Code-Along: Promisify `copy-json.js`
@@ -12,6 +12,7 @@ fs.readFile(inFile, { encoding: 'utf8' }, (error, content) => {
// 'Billy\nJames\nNick\n' --> ['Billy', 'James', 'Nick']

let lines = content.split('\n');

// clean up the array by removing the empty line
lines.pop();

@@ -5,14 +5,16 @@ let inFile = process.argv[2];

fs.readFile(inFile, { encoding: 'utf8' }, (error, content) => {
//first thing is check error
if (error){
if (error) {
console.error(error);
}

let lines = content.split('\n');

//clean up the array
lines.pop();

let randomLines = lines.sort(function(){
let randomLines = lines.sort(function () {
return 0.5 - Math.random();
});

0 comments on commit 2928752

Please sign in to comment.