Handling Asynchrony
JavaScript's internal event loop and the fact that all the code we write is "non-blocking" allows it to keep track of tons asynchronous processes while still operating on a single thread. We'll look at some low-level patterns for managing asynchrony and concurrency, then build all the way up to modern language features like async and await.
-
Handling AsynchronyWelcome
We'll recap everything we've covered so far, and review today's agenda.
-
Handling AsynchronyPromises
Promises can be thought of as "eventual values", and are a great abstraction of some asynchronous work. We'll review basic promise usage, error handling, and techniques for grouping promises together in sequence or in parallel.
-
Handling AsynchronyPROJECT: Fetch Coalescing
Often, when a single page app boots up, requests for remote data at various URLs will be sent out as part of the startup process. Occasionally, we can see multiple requests going out for the exact same resources. We'll build a utility that ensures that request for data that's already on its way are link together and resolved by the same promise.
-
Handling AsynchronyBreak
Coffee break
-
Handling AsynchronyIterators and Generator Functions
Several core JavaScript objects are “Iterables”, meaning they can provide an Iterator: special objects that maintain iteration state and can be asked for the next item in a sequence. Generator functions are simply functions that return iterators. We’ll look at these concepts in depth, and illustrate how they serve as the foundations for many higher-level JavaScript language features.
-
Handling AsynchronyPROJECT: Async Task Runner
One of the most powerful things we can build on top of generator functions is an “async task runner”. You have an “autocomplete” use case already set up, that involves running several async operations in sequence.
-
Handling AsynchronyAsync and Await
Now that we’ve done all the work to build a task function, we’re in a perfect position to appreciate a new language feature that looks very similar:
async
andawait
. Theawait
keyword, when used in anasync
function, allows us to write asynchronous code in a way that looks and feels very much like the synchronous (blocking) equivalent! -
Handling AsynchronyEXERCISE: Write an Integration Test
Integration tests are a perfect place to use
async
andawait
, because we often want to perform a series of simulated user interactions and wait for each one to complete, before proceeding further in the test. -
Handling AsynchronyLunch
Break for lunch