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

4 Asynchronous Control Flow Patterns with Callbacks

Moving from a synchronous programming style to a platform such as Node.js, where continuation-passing style (CPS) and asynchronous APIs are the norm, can be frustrating. Asynchronous code can make it hard to predict the order in which statements are executed. Simple problems such as iterating over a set of files, executing tasks in sequence, or waiting for a set of operations to complete require the developer to take on new approaches and techniques just to avoid ending up writing inefficient and unreadable code. When using callbacks to deal with asynchronous control flow, the most common mistake is to fall into the trap of callback hell and see the code growing horizontally, rather than vertically, with a nesting that makes even simple routines hard to read and maintain.

In this chapter, you will see how it's actually possible to tame callbacks and write clean, manageable asynchronous code by using some discipline and with the aid of some patterns. Knowing how to properly deal with callbacks will pave the way for adopting modern approaches such as promises and async/await.

In short, in this chapter, you will learn about:

  • The challenges of asynchronous programming.
  • Avoiding callback hell and other callback best practices.
  • Common asynchronous patterns such as sequential execution, sequential iteration, parallel execution, and limited parallel execution.
主站蜘蛛池模板: 哈密市| 赤峰市| 元江| 义乌市| 渝中区| 马龙县| 延长县| 博乐市| 龙口市| 五寨县| 阳东县| 高密市| 阜宁县| 沈阳市| 开平市| 嘉荫县| 清水县| 宣城市| 曲周县| 寻乌县| 宣武区| 仲巴县| 通山县| 龙里县| 尼玛县| 精河县| 安阳市| 宜章县| 华容县| 沂南县| 云龙县| 建昌县| 天津市| 云梦县| 治县。| 五华县| 东方市| 塔河县| 上思县| 苗栗县| 板桥市|