Concurrency and races · Chapter 7 of 15

Promise.all and partial writes

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.

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: leave a failed order half done on purpose

    Write reproduce() in repro.js. Build three fake services that keep one Set of what is still standing: kitchen.send adds 'kitchen' at once and kitchen.cancel deletes it; points.add rejects at once with an Error (say 'points service down') and points.remove deletes 'points'; riders.book waits for a promise you keep (Promise.withResolvers()), then adds 'rider', and riders.release deletes it. Start placeGroupOrder(order, { kitchen, points, riders }) from group-order.js with an order (any id) and catch its rejection, without waiting for it yet. Let everything waiting run, then let the rider booking finish, then wait for the order, let everything run again, and resolve to how many things are still standing.

    The check runs your reproduce() twice: beside the original group-order.js it must resolve to 2 (a kitchen ticket and a rider left for a failed order), and beside a fixed group-order.js it must resolve to 0. A fixed placeGroupOrder waits for the rider before it rejects, so release the rider before you wait for the order. No real waiting: reproduce() must finish within 2 seconds.

  2. Task 2

    group-order.js: all or nothing

    Fix placeGroupOrder(order, { kitchen, points, riders }) in group-order.js. It still starts the three steps together (kitchen.send, points.add, riders.book, each with the order). When all three work, it logs order <id>: placed, resolves to 'placed' and undoes nothing.

    When any step fails, it waits until every step has settled, including steps that finish after the failure, then undoes each step that worked, once, with the order (kitchen.cancel, points.remove, riders.release), never the steps that failed, then logs order <id>: failed, <reason> and rejects with an Error 'order <id> failed: <reason>'. <reason> is the message of the first step that failed, in the order kitchen, points, riders. Keep the two log lines. The check holds steps back and settles them by hand, with its own orders.

  3. Task 3

    group-order.test.js: a test that would have caught it

    Write a vitest test file, group-order.test.js, that would have caught this bug. The check runs your file three times: beside the original group-order.js, where at least one test must fail; beside the fixed group-order.js, where every test must pass; and beside a wrong fix that undoes only the steps that had finished when Promise.all gave up, where at least one test must fail.

    So make one step fail at once and hold another back so it finishes after the failure, then expect the order to reject, every step that worked (the late one too) to be undone once, and the failed step not to be undone. Fakes made with vi.fn remember their calls. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.js: leave a failed order half done on purpose
  • group-order.js: all or nothing
  • group-order.test.js: a test that would have caught it

More in Two requests at once

Build "Promise.all and partial writes" 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.