Node.js traps · Chapter 8 of 16
Request data in a module variable
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The event loop
- 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.
- Task 1
repro.js: put the wrong name on a receipt on purpose
Write reproduce() in repro.js. Start two bookings with withRequest from request-context.js: one with the context { id: 'req-81', member: 'Neha Bapat' } and one with { id: 'req-82', member: 'Pranav Joshi' }. Inside each, wait for that booking's own seat check (a promise you hold back with Promise.withResolvers), then give back the receipt's greeting: 'Dear ' followed by the member from currentRequest(). Start both bookings before releasing any seat check; then release Neha's, wait for her booking, release Pranav's, wait for his, and resolve to the two greetings, Neha's first.
The check runs your reproduce() twice: beside the original request-context.js it must resolve to ['Dear Pranav Joshi', 'Dear undefined'] (Neha's receipt got Pranav's name), and beside a fixed request-context.js to ['Dear Neha Bapat', 'Dear Pranav Joshi']. No real waiting and no setTimeout: reproduce() must finish within 2 seconds.
- Task 2
request-context.js: every request keeps its own context
Fix withRequest(ctx, fn) and currentRequest() in request-context.js, so every request sees its own context all the way through. Inside withRequest, currentRequest() must give ctx: at the start, after any await, inside a setImmediate or setTimeout callback and inside a .then that the request started. Two or three requests at once must each see their own ctx, whichever finishes first. A withRequest inside another gives the inner ctx inside it and the outer one after it. Outside every request, and after every request has finished or failed, currentRequest() gives undefined.
Keep the exports and what they give: withRequest resolves to what fn gives and rejects with fn's error; log(message) prints [<ctx.id>] <message> inside a request and just <message> outside one. The check uses its own contexts, holds three requests back at once and releases them in the order they started, then in the other order.
- Task 3
request-context.test.js: a test that would have caught it
Write a vitest test file, request-context.test.js, that would have caught the mix-up before the 9:30 rush. The check runs your file three times: beside the original request-context.js, where at least one test must fail; beside the fixed request-context.js, where every test must pass; and beside a wrong fix that saves the previous request and puts it back at the end, where at least one test must fail.
So start two requests with withRequest, hold both back with Promise.withResolvers, release the request that started first first, and check both sides: each request reads its own context from currentRequest() after its await. Each test must finish within 3 seconds.
What you will learn
- repro.js: put the wrong name on a receipt on purpose
- request-context.js: every request keeps its own context
- request-context.test.js: a test that would have caught it
More in The event loop
Build "Request data in a module variable" 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.