Concurrency and races · Chapter 6 of 15

Out-of-order responses

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

  • Part: Two requests at once
  • 3 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of Concurrency and races are free. This one comes with the all labs plan.

Why you need this

Two requests in flight are two separate trips. One may hit a slow server, a retry or a busy network, and the other may not. With a ping every 15 seconds, the first answer was nearly always back before the next question, so nothing overlapped. Every 3 seconds, with answers taking up to 4, two questions are in flight at once all evening.

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: show an old ETA on purpose

    Write reproduce() in repro.js. Build a fake maps service whose eta(location) does not answer at once: each call returns a promise you keep (Promise.withResolvers() gives a promise and its resolve), so you decide when each answer comes back. Make a tracker with createEtaTracker({ maps }) from eta.js and send two pings for one order (for example order 4410, first far away, then close by) before answering either. Answer the second ping with 5, then the first ping with 12, wait for both pings, and resolve to tracker.eta for that order.

    The check runs your reproduce() twice: beside the original eta.js it must resolve to 12 (the old answer shown), and beside a fixed eta.js it must resolve to 5. No real waiting: reproduce() must finish within 2 seconds.

  2. Task 2

    eta.js: only the newest ping's answer counts

    Fix createEtaTracker({ maps }) in eta.js. onPing(orderId, location) asks maps.eta(location); when the answer arrives, it is shown (eta(orderId) gives it, and the line order <id>: eta <minutes> min is logged) only if no newer ping was asked for the same order in the meantime. An answer to an older ping that arrives after a newer ping was asked is dropped: not shown and not logged.

    Each order keeps its own newest ping: a ping for one order never drops another order's answer. A newer ETA that is larger than the one shown (the rider hit traffic) is shown: newest wins, not smallest. Keep the log line and eta as they are. The check holds the maps answers back and releases them in every order, for more than one order.

  3. Task 3

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

    Write a vitest test file, eta.test.js, that would have caught this race. The check runs your file three times: beside the original eta.js, where at least one test must fail; beside the fixed eta.js, where every test must pass; and beside a wrong fix that keeps the smaller ETA, where at least one test must fail.

    So use a maps fake whose answers you hold back, and test both directions: an older ping answered after a newer one must not change the ETA, and a newer ping whose ETA is larger than the one shown must be shown. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.js: show an old ETA on purpose
  • eta.js: only the newest ping's answer counts
  • eta.test.js: a test that would have caught it

More in Two requests at once

Build "Out-of-order responses" yourself

Tapri's backend grew busy, and Kabir keeps handing you incidents that only happen when two things run at once. Each is a race 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 Concurrency and races are free. This one comes with the all labs plan.