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

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.
主站蜘蛛池模板: 高要市| 邻水| 苗栗市| 麦盖提县| 商城县| 喀什市| 平湖市| 赤水市| 杭州市| 灵川县| 庆城县| 杭锦后旗| 洪湖市| 华池县| 河津市| 北票市| 龙口市| 都匀市| 延长县| 咸阳市| 扎兰屯市| 扬州市| 瑞安市| 紫金县| 雅江县| 龙海市| 瓦房店市| 阳原县| 两当县| 连平县| 扎鲁特旗| 广宗县| 阿合奇县| 定州市| 来宾市| 土默特左旗| 特克斯县| 监利县| 祁连县| 恩施市| 濉溪县|