The callback pattern
Callbacks are the materialization of the handlers of the reactor pattern, which we introduced in the previous chapter. They are one of those imprints that give Node.js its distinctive programming style. Callbacks are functions that are invoked to propagate the result of an operation and this is exactly what we need when dealing with asynchronous operations. They do replace the use of the return
instruction that always executes synchronously. JavaScript is a great language to represent callbacks, because as we have seen, functions are first class objects and can be easily assigned to variables, passed as arguments, returned from another function invocation or stored into data structures. Another ideal construct for implementing callbacks is closures. With closures, we can in fact reference the environment in which a function was created; we can always maintain the context in which the asynchronous operation was requested, no matter when or where its callback is invoked...