Summary of Asynchrony: Under the Hood - Shelley Vohr - JSConf EU
Presentation Summary
In this presentation, Shelley Vohr, a software engineer at GitHub, provides an in-depth exploration of asynchronous programming in JavaScript. She begins by outlining the fundamental principles of asynchronicity, emphasizing the relationship between "now" and "later" in code execution, and the unpredictability of when tasks will run in the future.
Key Concepts Discussed
- Event Loop: Vohr explains the Event Loop as a single-threaded loop that manages code execution by processing tasks from a queue. When a task is scheduled to run later, it is added to this queue.
- Run-to-Completion Semantics: JavaScript ensures that the current task finishes executing before moving on to the next, which simplifies state management.
- Call Stack: The call stack tracks function calls and returns, maintaining the order of execution.
- Callbacks: Vohr discusses the challenges of using callbacks, particularly the potential for "callback hell," where the non-linear execution of functions complicates code readability and error handling.
- Promises: Introduced in ES6, Promises allow developers to handle asynchronous operations more effectively. They act as placeholders for future values and help manage the flow of asynchronous code without the pitfalls of callbacks.
- Microtasks Queue: Promises utilize a microtask queue to ensure that their callbacks are executed in a timely manner, even if the promise has already settled.
- Error Handling: Vohr highlights the importance of managing errors in asynchronous code, noting that unhandled promise rejections can lead to silent failures. Using
.catch()
is standard for error handling in Promises. - Generators: Generators offer a way to write asynchronous code that appears synchronous, allowing for pauses in execution and easier error handling with try-catch blocks.
- Async/Await: This modern approach simplifies the process of working with Promises, making asynchronous code more readable and allowing for straightforward error handling.
Overall, Vohr emphasizes that while callbacks, Promises, and Async/Await each have their strengths and weaknesses, the choice of which to use should depend on the specific requirements of the task at hand.
Main Speaker
- Shelley Vohr, Software Engineer at GitHub.
Notable Quotes
— 01:04 — « That, there, is the crux of asynch. »
— 06:11 — « Your brain operates sequentially. It places it at odds with the inherent functionality of callbacks. »
— 07:38 — « This paradigm is known as inversion of control, and can create significant trust issues. »
— 19:36 — « Asynch await is thus like promises non-blocking, and makes asynchronous code look and behave more like synchronous code. »
— 24:48 — « It is so easy to use asynch await that one of its slight dangers that you forget you're writing asynchronous code at all. »
Category
Technology