Rethinking Asynchronous JavaScript

Kyle Simpson

Kyle Simpson

You Don't Know JS
6 hours, 22 minutes CC
Rethinking Asynchronous JavaScript

Course Description

All JavaScript code has to manage asynchrony in some form or another. Effective async programming means knowing various different patterns and weaving them together to make not only workable but readable and understandable code. In this course, we start from the beginning, rebuilding and rethinking why we async, and how. We solve the same problem over and over, each time with a different async pattern. By the end, we've seen and practices all the major async patterns, so we have a more concrete sense of the pros/cons.

This course and others like it are available as part of our Frontend Masters video subscription.

Preview
Close

Course Details

Published: March 29, 2016

Learn Straight from the Experts Who Shape the Modern Web

Your Path to Senior Developer and Beyond
  • 200+ In-depth courses
  • 18 Learning Paths
  • Industry Leading Experts
  • Live Interactive Workshops
Get Unlimited Access Now

Table of Contents

Parallel vs. Async

Section Duration: 25 minutes
  • Course Introduction
    Kyle Simpson (@getify) begins his course on Async Patterns with a brief introduction of himself and an overview of the course agenda. He runs a few open source projects like LabJS, Grips, and Asynquence. He is also the head of curriculum for the developer/engineer training school Maker Square.
  • Single Threaded JavaScript
    In the context of programming, Parallelism is the utilization of multiple threads in an operating system. Routines are able to run at the same time regardless of execution order. JavaScript, however, is single threaded and only one line of code can be executed at any given time. Kyle explains these concepts to build a basis for understanding asynchronous programming.
  • Concurrency
    Kyle introduces concurrency as two higher-level tasks happening within the same timeframe. He illustrates these higher-level tasks or macro-tasks might be composed of many micro-tasks. As each micro-task is initiated, it queues up behind any other existing tasks and is executed in order.

Callback

Section Duration: 46 minutes

Thunks

Section Duration: 32 minutes

Promises

Section Duration: 1 hour, 49 minutes

Generators

Section Duration: 1 hour, 5 minutes
  • Generator Example
    Generators are a new type of function in ES6. In asynchronous programming, generators solve the non-sequential, non-reasonable issues of callbacks. When a generator is called, it returns an iterator which will step through the generator, pausing anytime the yield keyword is encountered. After introducing the concept of a generator, Kyle shares a simple code example.
  • Messaging
    If the yield keyword in a generator is called with a value, that value will be returned to the iterator along with a “done” boolean value. This allows messages to be sent outside of the generator. Kyle explains why this is useful and also demonstrates that subsequent calls to the next() method can be used to pass message back into the generator.
  • Messaging Questions
    Kyle spends a few minutes reviewing the way generators send and receive messages. He also answers a number of audience questions about generators, iterators, and the use of the yield keyword.
  • Async Generators
    Kyle demonstrates how generators can be used in asynchronous applications. The yield keyword will block a generator while an async call is being executed. Values returned from the async call can be passed back to the generator allowing it to resume and sequentially trigger other async operations.
  • Promises + Generators
    While generators completely solve the issue of sequencing asynchronous operations, they are still susceptible to inversion of control. Since promises solve the inversion of control issue, Kyle explains how they should be combined with generators to create better asynchronous code.
  • Exercise 7
    In this exercise, you will now use generators to write the async flow control code from the previous exercises.
  • Exercise 7 Solution
    Kyle walks through the solution to exercise 7.
  • Quiz
    Kyle spends a few minutes quizzing the audience on the async patterns he’s covered up to this point.

Observables

Section Duration: 49 minutes

CSP

Section Duration: 52 minutes

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now