The Callback pattern
Callbacks are the embodiment of the Reactor pattern’s handlers (which we covered in Chapter 1, The Node.js Platform), a core component of the Node.js architecture.
Callbacks can be defined as functions triggered to handle the result of an operation — exactly what we need when working with asynchronous tasks. In an asynchronous context, callbacks take the place of the typical return statement, which only works synchronously. JavaScript is particularly well-suited for callbacks, as functions are first-class objects. This allows them to be easily assigned to variables, passed as arguments, returned from other functions, or stored in data structures.
Callbacks played a key role in shaping Node.js’s distinctive programming style for many years. Despite their decline in popularity in modern user-facing code, they remain a fundamental and indispensable component of both the Node.js and JavaScript ecosystems. In fact, callbacks are the building...