Skip to content

Commit 286f59a

Browse files
committed
Use promises
1 parent 2fb5e1d commit 286f59a

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

README.md

+43-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ JavaScript software. Enjoy!
88
1. [Variables](#variables)
99
2. [Functions](#functions)
1010
3. [Classes](#classes)
11-
4. [Comments](#comments)
11+
4. [Concurrency](#concurrency)
12+
5. [Comments](#comments)
1213

1314
## **Variables**
1415
### Use meaningful and pronounceable variable names
@@ -398,6 +399,47 @@ class Human extends Mammal {
398399
```
399400
**[⬆ back to top](#table-of-contents)**
400401

402+
## **Concurrency**
403+
### Use Promises, not callbacks
404+
Callbacks aren't clean, and they cause excessive amounts of nesting. With ES6,
405+
Promises are a built-in global type. Use them!
406+
407+
**Bad:**
408+
```javascript
409+
require('request').get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin', function(err, response) {
410+
if (err) {
411+
console.error(err);
412+
}
413+
else {
414+
require('fs').writeFile('article.html', response.body, function(err) {
415+
if (err) {
416+
console.error(err);
417+
} else {
418+
console.log('File written');
419+
}
420+
})
421+
}
422+
})
423+
424+
```
425+
426+
**Good**:
427+
```javascript
428+
require('request-promise').get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin')
429+
.then(function(response) {
430+
return require('fs-promise').writeFile('article.html', response);
431+
})
432+
.then(function() {
433+
console.log('File written');
434+
})
435+
.catch(function(err) {
436+
console.log(err);
437+
})
438+
439+
```
440+
**[⬆ back to top](#table-of-contents)**
441+
442+
401443
## **Comments**
402444
### Only comment things that have business logic complexity.
403445
Comments are an apology, not a requirement. Good code *mostly* documents itself.

0 commit comments

Comments
 (0)