JavaScript traps · Chapter 11 of 16

Unhandled promise rejections

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.

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 receipt unhandled

    Write reproduce() in repro.js. Build a fake db whose save(order) resolves to an order id, a fake sms whose send(phone, text) rejects with new Error('SMS gateway timeout'), and a log object with info(text) and error(text) functions. Call placeOrder(order, { db, sms, log }) from checkout.js with an order ({ phone, total }) and the fakes, and resolve to the id placeOrder resolved to.

    The check runs your reproduce() twice, each time in a separate node process that records every promise rejection nobody handled: beside the original checkout.js there must be one with the message 'SMS gateway timeout', and beside a fixed checkout.js there must be none, and reproduce() must resolve to the same id both times. So it must really call placeOrder with a gateway that fails, and leave no rejection of its own. No real waiting: the fakes answer straight away, and reproduce() must finish within 2 seconds.

  2. Task 2

    checkout.js: a failed receipt never takes the server down

    Fix placeOrder(order, { db, sms, log }) in checkout.js so a failed receipt is handled and logged, and checkout still does not wait for the SMS. When the SMS gateway rejects, placeOrder still resolves to the id db.save gave, log.error is called once with exactly order <id>: receipt not sent, <the error's message>, and no rejection is left unhandled. When the gateway is slow, placeOrder resolves to the id before the SMS has answered. When it works, the receipt goes to order.phone with sendReceipt's text, and nothing goes to log.error.

    The check runs the failing gateway in a separate node process that records rejections nobody handled, and holds a slow gateway's answer back. Keep sendReceipt, its text and the log.info line as they are. Awaiting the receipt hides the crash, but then checkout fails whenever the SMS gateway does, and waits for it when it is slow; a global unhandledRejection listener does not handle this promise either.

  3. Task 3

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

    Write a vitest test file, checkout.test.js, that would have caught this bug before the rush. The check runs your file three times: beside the original checkout.js, where at least one test must fail; beside the fixed checkout.js, where every test must pass; and beside a wrong fix that awaits the receipt, where at least one test must fail.

    vitest reports a rejection nobody handled, but it does not count it as a failed test, so your test must fail through an expect: with an SMS gateway that rejects, expect placeOrder to resolve to the order id, and expect log.error to have been called with the receipt line, after letting the receipt's promise settle. Each test must finish within 3 seconds.

What you will learn

  • repro.js: leave a failed receipt unhandled
  • checkout.js: a failed receipt never takes the server down
  • checkout.test.js: a test that would have caught it

More in Time

Build "Unhandled promise rejections" 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.