- 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.
- GitLab Cookbook
- C語言程序設計(第2 版)
- Beginning Java Data Structures and Algorithms
- C#程序設計實訓指導書
- aelf區塊鏈應用架構指南
- Java項目實戰精編
- Python編程從0到1(視頻教學版)
- ExtJS高級程序設計
- C#開發案例精粹
- Cocos2d-x Game Development Blueprints
- 測試架構師修煉之道:從測試工程師到測試架構師
- Python Digital Forensics Cookbook
- Learning C++ by Creating Games with UE4
- Mastering OpenStack
- Magento 2 Developer's Guide