- 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.
- Building a RESTful Web Service with Spring
- 區(qū)塊鏈架構(gòu)與實現(xiàn):Cosmos詳解
- 算法精粹:經(jīng)典計算機科學(xué)問題的Python實現(xiàn)
- Python面向?qū)ο缶幊蹋簶?gòu)建游戲和GUI
- C語言程序設(shè)計
- Building Android UIs with Custom Views
- Instant Lucene.NET
- Python深度學(xué)習(xí):模型、方法與實現(xiàn)
- OpenStack Networking Essentials
- Kubernetes進階實戰(zhàn)
- IDA Pro權(quán)威指南(第2版)
- 大學(xué)計算機基礎(chǔ)實驗指導(dǎo)
- After Effects CC案例設(shè)計與經(jīng)典插件(視頻教學(xué)版)
- 嵌入式C編程實戰(zhàn)
- JavaScript Concurrency