JavaScript traps · Chapter 10 of 16

forEach with async

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

Every send still happens, so the messages arrive and the log looks busy. A try around the forEach finishes the moment forEach returns, long before a send fails, so it never catches anything. The rejection is left with nobody handling it (case 11 is about where that goes).

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: a summary that comes before the messages

    Write reproduce() in repro.js. Make three riders, each { name, phone }, and a fake sms object whose send(phone, text) is an async function that resolves straight away. Call notifyAll(riders, sms) from notify.js, wait for it, and resolve to the sent count in the summary it resolved to.

    The check runs your reproduce() twice: beside the original notify.js it must resolve to 0 (the summary was made before any send had answered), and beside a fixed notify.js it must resolve to 3. So it must really call notifyAll with three riders whose sends succeed. No real waiting: the fake answers straight away, and reproduce() must finish within 2 seconds.

  2. Task 2

    notify.js: one SMS at a time, and a true summary

    Fix notifyAll(riders, sms) in notify.js so it sends one SMS at a time, in the riders' order, each only after the one before has answered. A send that resolves counts as sent and logs sms to <name>: sent. A send that rejects counts as failed, logs sms failed: <the error's message>, and the next rider still gets theirs. After the last answer, notifyAll logs all riders notified: <sent> sent, <failed> failed and resolves to { sent, failed }. It never rejects because an SMS failed, and with no riders it resolves to { sent: 0, failed: 0 }.

    The check holds every answer back and looks: until the first rider's send answers, no other send may start and nothing may be logged or resolved. Keep shiftText and the three log lines as they are. A fix that sends to every rider at once is as wrong as the bug: the gateway takes one message at a time.

  3. Task 3

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

    Write a vitest test file, notify.test.js, that would have caught this bug before it shipped. The check runs your file three times: beside the original notify.js, where at least one test must fail; beside the fixed notify.js, where every test must pass; and beside a wrong fix that sends to every rider at once with Promise.all, where at least one test must fail.

    So hold the gateway's answers back with a fake send that hands back a promise you settle yourself: start notifyAll, check that only the first send has started and notifyAll has not resolved, answer it, check the second starts, and after the last answer check what notifyAll resolves to. Each test must finish within 3 seconds.

What you will learn

  • repro.js: a summary that comes before the messages
  • notify.js: one SMS at a time, and a true summary
  • notify.test.js: a test that would have caught it

More in Time

Build "forEach with async" 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.