Permalink
Browse files

Ruined example and formatting

I went back and looked at this example, and I think I goofed. I think
this one might be better. I need 👀
  • Loading branch information...
payne-chris-r
payne-chris-r committed May 8, 2017
1 parent 8b74380 commit e9aa337fcf9d9b1a1da99fcba9aab482e42b86c3
Showing with 23 additions and 14 deletions.
  1. +21 −12 README.md
  2. +1 −1 snippets/readJSON-promise-1.js
  3. +1 −1 snippets/readJSON-promise.js
@@ -110,7 +110,7 @@ const readJSON = function (filename) { // <-- look here
readJSON('./example.json')
.then((pojo) => {
delete pojo.key // modify object
modifierFunc(pojo) // modify object
return pojo // explicitly returns pojo
})
.catch((err) => { // handle error conditions
@@ -137,45 +137,54 @@ const readJSON = function (filename) {
}
readJSON('./example.json')
.then(callback) // modify object --> returns what callback(prev) returns
.then(modifierFunc) // modify object --> returns what callback(prev) returns
.catch(console.error) // handle error conditions
```
<!-- end code block -->

### Rules for Promisifying Your Code:
1. Only ever pass a function as a call back. NEVER pass a function invocation!
## Rules for Promisifying Your Code:
#### 1. Only ever pass a function as a call back. NEVER pass a function invocation!
<details>
<summary>*Why?*</summary>
<summary>Why?</summary>
The difference is `.then()` _expects_ a callback. If you invoke the function, the `.then` gets a _value_ NOT a function to invoke later.
</details>

<!-- start code block file="snippets/callback-vs-invocation.js" -->
```js
.then(JSON.parse)
// vs
.then(JSON.parse(json))
```
<!-- end code block -->
1. ALWAYS consider WHAT is being return from each block of a promise chain. Be explicit if you need to!

#### 2. ALWAYS consider WHAT is being return from each block of a promise chain. Be explicit if you need to!
<details>
<summary>*Why?*</summary>
<summary>Why?</summary>
Because some methods don't return what you expect them to. If you're ever unsure, BE SURE by making it explicit!
</details>
1. Safety third <details>
<summary>*Why?*</summary>

#### 3. Safety third
<details>
<summary>Why?</summary>
'cause it should never be first or second.
</details>
1. Indent once at the start, and then lineup your `.then`s and `.catch`s <details>
<summary>*Why?*</summary>

#### 4. Indent once at the start, and then lineup your `.then`s and `.catch`s
<details>
<summary>Why?</summary>
Because formatting is important to humans.
</details>

<!-- start code block file="snippets/format.js" -->
```js
startingFunction()
.then(JSON.parse)
.catch(console.error)
```
<!-- end code block -->
1. Never nest `.then`s--return out, and continue with the next `.then` in-line. *Why?* Because nesting `.then`s defeats the purpose of promises.

#### 5. Never nest `.then`s--return out, and continue with the next `.then` in-line. Why? Because nesting `.then`s defeats the purpose of promises.

<!-- start code block file="snippets/nesting.js" -->
```js
startingFunction()
@@ -12,5 +12,5 @@ const readJSON = function (filename) {
}

readJSON('./example.json')
.then(callback) // modify object --> returns what callback(prev) returns
.then(modifierFunc) // modify object --> returns what callback(prev) returns
.catch(console.error) // handle error conditions
@@ -15,7 +15,7 @@ const readJSON = function (filename) { // <-- look here

readJSON('./example.json')
.then((pojo) => {
delete pojo.key // modify object
modifierFunc(pojo) // modify object
return pojo // explicitly returns pojo
})
.catch((err) => { // handle error conditions

0 comments on commit e9aa337

Please sign in to comment.