@@ -8,7 +8,8 @@ JavaScript software. Enjoy!
8
8
1 . [ Variables] ( #variables )
9
9
2 . [ Functions] ( #functions )
10
10
3 . [ Classes] ( #classes )
11
- 4 . [ Comments] ( #comments )
11
+ 4 . [ Concurrency] ( #concurrency )
12
+ 5 . [ Comments] ( #comments )
12
13
13
14
## ** Variables**
14
15
### Use meaningful and pronounceable variable names
@@ -398,6 +399,47 @@ class Human extends Mammal {
398
399
```
399
400
** [ ⬆ back to top] ( #table-of-contents ) **
400
401
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
+
401
443
## ** Comments**
402
444
### Only comment things that have business logic complexity.
403
445
Comments are an apology, not a requirement. Good code * mostly* documents itself.
0 commit comments