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

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.
主站蜘蛛池模板: 平定县| 衡阳市| 伊川县| 灵台县| 灵台县| 昌都县| 五华县| 潢川县| 永康市| 湟源县| 新野县| 呼伦贝尔市| 视频| 荣昌县| 泰顺县| 金堂县| 永丰县| 商南县| 收藏| 晋中市| 宿松县| 佛冈县| 乐至县| 石柱| 肇源县| 怀化市| 仁怀市| 泽库县| 隆回县| 凌云县| 佛冈县| 高雄市| 金山区| 大竹县| 福贡县| 阳江市| 文成县| 昌图县| 阿拉尔市| 嵩明县| 吉首市|