Events. How do they work? Confusion about JavaScript’s asynchronous event model is as old as JavaScript itself. Confusion leads to bugs, bugs lead to anger, and Yoda taught us the rest….
Yet at heart, JavaScript events are both conceptually elegant and practical. Once you’ve accepted the language’s single-threaded design, it feels like a feature rather than a limitation. It means that your code is uninterruptible and that the events you schedule line up in an orderly fashion.
In this chapter, we’ll take a tour of JavaScript’s asynchronous mechanisms and dispel some common misconceptions. We’ll see what setTimeout really does. Then we’ll discuss handling errors in callbacks. Finally, we’ll set up the main theme of this book: organizing async code for clarity and maintainability.
3.139.104.23