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

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.
主站蜘蛛池模板: 邵阳县| 茶陵县| 东辽县| 乐安县| 贵溪市| 美姑县| 北海市| 峨山| 炎陵县| 会泽县| 江陵县| 德阳市| 镇巴县| 筠连县| 杭州市| 马边| 凤阳县| 乐陵市| 吴旗县| 苗栗县| 突泉县| 南丹县| 木兰县| 洞头县| 额尔古纳市| 红桥区| 延长县| 乡宁县| 浦县| 时尚| 临桂县| 大田县| 广南县| 平邑县| 壤塘县| 南丰县| 湛江市| 钦州市| 永登县| 云浮市| 大名县|