官术网_书友最值得收藏!

What promises prescribed not to do

Promises clearly outline what not to do while implementing a promises paradigm. We saw most of these rules in Chapter 2, The JavaScript Asynchronous Model. Let's take a look at these from the promises paradigm in order to refresh our memories.

The following two practices must be taken into account while implementing promises, regardless of what implementation you are using:

  • Avoiding getting into a callback hell
  • Avoiding use of unnamed promises

Avoiding getting into callback hell

We are already aware what callbacks are and how to handle them. Callbacks are a great way to implement an asynchronous model, but they have their own cost. They are unmanageable at some point, and that point comes in when you start your descent in callbacks. The deeper you pe in, the more difficult it becomes to handle, thus leading you into a callback hell scenario.

All of the promises implementations have sorted this problem very simply and wisely.

Avoiding getting into callback hell

A handy way to tackle callback hell

Avoiding the use of unnamed promises

As we saw from Chapter 2, The JavaScript Asynchronous Model, the use of unnamed promises can cause huge problems and will cost more time than the normal function of writing and testing. In some instances, it's good and recommended that you do not give the name of your function, but it's not good practice to leave your promise unnamed.

If someone thinks anonymous functions are hard to deal with, then unreasonably named functions are hard to understand and maintain. I recommend that you come up with a proper, predecided naming convention, and it should be done well before writing the actual code. I prefer to use CamelCase notation in Microsoft style, in which the starting name of the function is in lowercase and the connecting name is in uppercase.

主站蜘蛛池模板: 桐乡市| 吉林省| 乌兰察布市| 延安市| 保德县| 缙云县| 永吉县| 墨竹工卡县| 郸城县| 库伦旗| 亚东县| 资中县| 凤台县| 屯门区| 什邡市| 汽车| 麟游县| 乐业县| 梁山县| 雅江县| 阿鲁科尔沁旗| 定日县| 囊谦县| 新津县| 古交市| 方城县| 霍城县| 丹阳市| 沿河| 齐河县| 长阳| 于都县| 齐齐哈尔市| 喜德县| 泰和县| 商河县| 原阳县| 聊城市| 临海市| 东台市| 凤阳县|