Reasoning about asynchronous code can be hard if you don’t understand the mechanics. It often trips up programmers with deep experience in other languages when they try their hand at client or server-side JavaScript. But, asynchronous JavaScript isn’t as hard as it seems once you learn a few simple rules about how it works. In this talk, we’ll dive into the mechanics of concepts that you might have had a chance to full grok.
Let’s dispel this confusion once and for all. We’ll explore some metaphors that illustrate the difference between how synchronous and asynchronous code execute. In a 30-minute tour de force, we’ll dive into essential concepts like the call stack and the event loop to build a solid conceptual understanding of how the asynchronous model is implemented. We’ll take a good hard look at a number of common patterns for writing asynchronous code in JavaScript. We’ll start with listening for events on XMLHttpRequest before abstracting that mess with callbacks and promises. We’ll pull back the curtain and see that async
/await
is really just a cocktail of promises and generators. Finally, we’ll lightly touch upon some more boutique approaches like CSP and shooting messages back and forth between the main and worker threads.