Asynchronous JavaScript: Waiting Without Freezing

Timers, promises, async/await and fetching data from the internet. Ten short pages from Freshman Year of College, about 18 minutes of reading in all.

What you’ll learn

  • Synchronous vs asynchronous code
  • setTimeout and setInterval
  • Callbacks and the Pyramid of Doom
  • Promises: pending, fulfilled, rejected
  • then, catch and finally
  • async and await
  • Running things in parallel with Promise.all
  • Fetching real data from the internet
  • How the event loop works

The pages

Ten pages, about 18 minutes.

  1. Synchronous vs Asynchronous Let's visit a restaurant. Restaurant A has a very strange waiter. He comes to your table and takes your order. Then he…
  2. Timers: setTimeout and setInterval The simplest way to see asynchronous code in action is with a timer. setTimeout(callback, delay) runs a function once…
  3. Callbacks and the Pyramid of Doom Before 2015, the main way to handle asynchronous tasks was with callbacks: you pass a function that should be called…
  4. Promises: An IOU for a Future Value Ever been to a restaurant where they give you a little buzzer when you order? The buzzer isn't your food. But it's a…
  5. async and await: Code That Reads Like a Story Promises were a huge improvement. But in 2017, JavaScript added something even nicer: async and await. They let you…
  6. Doing Things in Parallel: Promise.all Using await one after another runs tasks in sequence: each one waits for the previous one to finish. That's what you…
  7. Fetching Data From the Internet Now for the big payoff. All of this asynchronous stuff exists mainly so that JavaScript can do one incredibly…
  8. The Event Loop: How It All Works Okay, time for the big reveal. How does single-threaded JavaScript juggle timers, clicks and network requests all at…
  9. Mini Project: The Burger Kitchen Simulator Let's simulate a busy restaurant kitchen. Some steps must happen in order, some can happen at the same time, and…
  10. Recap The key ideas of Freshman Year, Asynchronous JavaScript, on one page. Synchronous code runs one line at a time…

Reading is step one

CurlyScript turns these notes into a game: short questions, an explanation after every answer, and a test at the end of each grade. Free, with nothing to sign up for.

Start learning