File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -439,6 +439,45 @@ require('request-promise').get('https://en.wikipedia.org/wiki/Robert_Cecil_Marti
439439```
440440** [ ⬆ back to top] ( #table-of-contents ) **
441441
442+ ### Async/Await are even cleaner than Promises
443+ Promises are a very clean alternative to callbacks, but ES7 brings async and await
444+ which offer an even cleaner solution. All you need is a function that is prefixed
445+ in an ` async ` keyword, and then you can write your logic imperatively without
446+ a ` then ` chain of functions. Use this if you can take advantage of ES7 features
447+ today!
448+
449+ ** Bad:**
450+ ``` javascript
451+ require (' request-promise' ).get (' https://en.wikipedia.org/wiki/Robert_Cecil_Martin' )
452+ .then (function (response ) {
453+ return require (' fs-promise' ).writeFile (' article.html' , response);
454+ })
455+ .then (function () {
456+ console .log (' File written' );
457+ })
458+ .catch (function (err ) {
459+ console .log (err);
460+ })
461+
462+ ```
463+
464+ ** Good** :
465+ ``` javascript
466+ async function getCleanCodeArticle () {
467+ try {
468+ var request = await require (' request-promise' )
469+ var response = await request .get (' https://en.wikipedia.org/wiki/Robert_Cecil_Martin' );
470+ var fileHandle = await require (' fs-promise' );
471+
472+ await fileHandle .writeFile (' article.html' , response);
473+ console .log (' File written' );
474+ } catch (err) {
475+ console .log (err);
476+ }
477+ }
478+ ```
479+ ** [ ⬆ back to top] ( #table-of-contents ) **
480+
442481
443482## ** Comments**
444483### Only comment things that have business logic complexity.
You can’t perform that action at this time.
0 commit comments