- Node.js Design Patterns
- Mario Casciaro Luciano Mammino
- 226字
- 2021-06-18 18:15:08
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.
- 精通Nginx(第2版)
- 精通JavaScript+jQuery:100%動態網頁設計密碼
- OpenShift開發指南(原書第2版)
- 新一代通用視頻編碼H.266/VVC:原理、標準與實現
- CentOS 7 Server Deployment Cookbook
- JavaScript從入門到精通(第3版)
- 算法訓練營:提高篇(全彩版)
- 編程數學
- Asynchronous Android Programming(Second Edition)
- Active Directory with PowerShell
- Scala Reactive Programming
- PHP從入門到精通(第4版)(軟件開發視頻大講堂)
- D3.js By Example
- Tableau Desktop可視化高級應用
- 奔跑吧 Linux內核