- 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.
- C語言程序設計教程
- Java應用與實戰
- Microsoft Exchange Server PowerShell Cookbook(Third Edition)
- ThinkPHP 5實戰
- Python測試開發入門與實踐
- 神經網絡編程實戰:Java語言實現(原書第2版)
- Java深入解析:透析Java本質的36個話題
- FLL+WRO樂高機器人競賽教程:機械、巡線與PID
- Visual Basic程序設計
- Learning Concurrency in Kotlin
- Tableau 10 Bootcamp
- Multithreading in C# 5.0 Cookbook
- 從零開始學Linux編程
- Scratch3.0趣味編程動手玩:比賽訓練營
- Odoo 10 Implementation Cookbook