Learn JavaScript

Lesson 4 of 9 ¡ Async 1: Callbacks and the Event Loop

Module 12 ¡ Async 1: Callbacks and the Event Loop

When to Use Which: a Callback, a Promise or an Event, and setTimeout, setInterval or requestAnimationFrame

Free with an accountReading

Maria is planning the async parts of a delivery app. She needs a price from the server once. She needs to know every time a rider's location changes. She needs the map to redraw smoothly, and an "orders" badge to refresh every thirty seconds. She knows the tools from Lessons 01 to 03. What she does not know yet is which tool fits which job.

Checking what this lesson needs for youâ€Ļ

What is inside When to Use Which: a Callback, a Promise or an Event, and setTimeout, setInterval or requestAnimationFrame

  1. One answer, one listener: a callback
  2. One answer, passed around: a promise
  3. Many answers, many listeners: an event
  4. Callback, promise or event: the table
  5. setInterval or a chain of setTimeout
  6. requestAnimationFrame, in the browser
  7. setTimeout 0 or queueMicrotask
  8. The flowchart