JavaScript traps · Chapter 12 of 16

The event loop order

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Time
  • 3 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.

Why you need this

Here is a loyalty banner of the same kind: one update on a timer, a later one on a promise.

const points = { get: async (phone) => 120 };

function showPoints(phone, show) {
  setTimeout(() => show('loading points...'), 0);
  points.get(phone).then((n) => show(`${n} points`));
}

showPoints('98450 12345', (text) => console.log('banner:', text));
banner: 120 points
banner: loading points...

The points service answered at once, so its promise callback is a microtask and jumps the queue: the older text is drawn last. With a service that takes 300 ms to answer, the same code prints the two lines the right way round. It is correct only while the other side is slow, and nothing warns you the day it gets fast.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.js: make the status flip back on purpose

    Write reproduce() in repro.js. Build a fake kitchen, an object whose accept(order) resolves to true at once (an async function that just returns), and a render function that pushes each status it is given into a list. Call trackOrder(order, { kitchen, render }) from status.js with an order (any id) and the two fakes, wait until the screen's timers and promise callbacks have all run, and resolve to the list.

    The check runs your reproduce() twice: beside the original status.js it must resolve to [ 'preparing', 'placed' ] (the older status drawn last), and beside a fixed status.js to [ 'placed', 'preparing' ]. So it must really call trackOrder and wait. No real waiting: a timer of your own of 0 ms, set after trackOrder's, is enough, and reproduce() must finish within 2 seconds.

  2. Task 2

    status.js: statuses in the order they happened

    Fix trackOrder(order, { kitchen, render }) in status.js so the customer sees the statuses in the order they happened: render is called with 'placed' first, as soon as the order is tracked, and then with 'preparing', once kitchen.accept(order) has resolved. Each status is drawn once, and the log line order <id>: screen shows <status> is printed for each, as it is today.

    The order must be right whether the kitchen answers at once, answers later, or answers after a 0 ms timer of its own: the check runs all three with vitest's fake timers, and holds the kitchen's answer back to see that 'placed' is already on screen before the kitchen answers. The check uses its own orders, not 3120, and a fix that never shows 'placed' is as wrong as the bug.

  3. Task 3

    status.test.js: a test that would have caught it

    Write a vitest test file, status.test.js, that would have caught this bug before the kitchen got fast. The check runs your file three times: beside the original status.js, where at least one test must fail; beside the fixed status.js, where every test must pass; and beside a wrong fix that keeps the timer and skips 'placed' once 'preparing' is on screen, where at least one test must fail.

    So check the whole list of statuses drawn, in order: with a kitchen that answers at once it must be [ 'placed', 'preparing' ]. Holding the kitchen's answer back and checking that 'placed' is already drawn makes a stronger guard. Use fake timers, with no real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.js: make the status flip back on purpose
  • status.js: statuses in the order they happened
  • status.test.js: a test that would have caught it

More in Time

Build The event loop order yourself

Tapri runs on JavaScript, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.