Node.js traps · Chapter 16 of 16

A request with no timeout

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

  • Part: Modules
  • 3 tasks
  • Node.js
  • In the all labs plan

The first 2 chapters of Node.js 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: keep a refund waiting on purpose

    Write reproduce() in repro.js. Make a pretend clock with createClock() from fake-clock.js and a gateway with createGateway() from gateway.js, and call gateway.hang() so it takes the request and never answers. Make the refunds with createRefunds({ fetch: gateway.fetch, clock }) from refunds.js, leaving timeoutMs at its default of 10,000. Start one refund (any booking id, say 'B-2041') without awaiting it, and keep a word for its state: 'still waiting' to begin with, 'refunded' when it resolves, 'timed out' when it rejects. Then move the clock on 10 seconds with await clock.tick(10_000), and resolve to the word.

    The check runs your reproduce() twice: beside the original refunds.js it must resolve to 'still waiting' (nothing ever gave up), and beside a fixed refunds.js to 'timed out'. Never await the refund itself: beside the original code it never settles. No real waiting: the pretend clock moves 10 seconds at once, and reproduce() must finish within 2 seconds.

  2. Task 2

    refunds.js: refunds that give up on a silent gateway

    Fix createRefunds({ fetch, clock, timeoutMs }) in refunds.js so refund(bookingId) gives up on a gateway that does not answer within timeoutMs (10,000 by default), and closes the request when it does. When the gateway answers in time, refund resolves to its refundId, logs refunds: <id> refunded (<refundId>) and leaves no timer on the clock. When it answers with a status that is not ok, refund rejects with the Error refund <id>: gateway said <status>, and leaves no timer. When it does not answer, nothing happens before timeoutMs; at timeoutMs the request is aborted through the signal passed to fetch, refund rejects with the Error refund <id>: gateway timed out after <timeoutMs> ms, and no request and no timer is left.

    Keep the exports, the URL, the POST, the log line and the messages as they are. Use the clock you are given (clock.setTimeout and clock.clearTimeout), never the global timers or AbortSignal.timeout, which use real time. The check uses its own gateway, which holds every answer back, counts the requests still open and moves a pretend clock.

  3. Task 3

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

    Write a vitest test file, refunds.test.js, that would have caught the stuck refunds before they reached production. The check runs your file three times: beside the original refunds.js, where at least one test must fail; beside the fixed refunds.js, where every test must pass; and beside a wrong fix that races the request against a timer, so refund rejects but the request is never aborted, where at least one test must fail.

    So use the pretend clock and a gateway that hangs, start a refund without awaiting it and keep its outcome, move the clock past timeoutMs, and check both sides: the refund rejected with the timed out message, and gateway.open() is 0. Each test must finish within 3 seconds.

What you will learn

  • repro.js: keep a refund waiting on purpose
  • refunds.js: refunds that give up on a silent gateway
  • refunds.test.js: a test that would have caught it

More in Modules

Build "A request with no timeout" yourself

Parda's booking backend has run on Node for a year, and Nikhil keeps handing you incidents where Node did exactly what the team told it. Each is a runtime 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 Node.js traps are free. This one comes with the all labs plan.