JavaScript traps · Chapter 6 of 16
this in callbacks
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Everyday traps
- 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.
- Task 1
repro.js: make the kitchen screen's tick fail on purpose
Write reproduce() in repro.js, with no real waiting. Make a KitchenQueue from kitchen.js for an outlet (any name) and add two orders, each { id }. Start it with a fake schedule(fn, ms) that keeps fn and does not run it (it may return any handle). Then call the kept function once yourself, on its own with nothing in front of it, the way setInterval does, inside a try. If it throws, resolve to the error's message; if not, resolve to { queue, ready }: the ids of the orders still in the queue and of the ready ones.
The check runs your reproduce() twice: beside the original kitchen.js it must resolve to "Cannot read properties of undefined (reading 'queue')" (the error from the ticket), and beside a fixed kitchen.js it must resolve to { queue, ready } with one id in ready and at least one still in the queue. So it must really call the function start handed your schedule. reproduce() must finish within 2 seconds.
- Task 2
kitchen.js: keep each queue's this when the screen ticks
Fix KitchenQueue in kitchen.js so that once start(schedule) has run, every call of the function it handed schedule (every 5000 ms) is a tick of that same queue: it moves the first order in this queue's own queue to its own ready list and logs <outlet>: order <id> ready, and does nothing when the queue is empty. Two queues started at once never mix: each tick works on its own queue's orders. stop() stops the ticks. schedule defaults to setInterval, and stop's cancel to clearInterval.
Keep add, stop, tick and the log line as they are. The check passes its own schedule, keeps the function and calls it on its own, as setInterval does, with two queues at once, and also tries the real setInterval with fake timers. A fix that keeps the queue in a variable outside the class works for one queue, not for two.
- Task 3
kitchen.test.js: a test that would have caught it
Write a vitest test file, kitchen.test.js, that would have caught this bug. The check runs your file three times: beside the original kitchen.js, where at least one test must fail; beside the fixed kitchen.js, where every test must pass; and beside a wrong fix that keeps the queue in one variable for the whole module (so every tick works on the queue started last), where at least one test must fail.
So start two queues, each with its own orders and its own fake schedule, call each kept function on its own, as in your repro, and check that each queue's first order, and only that queue's, is ready. Each test must finish within 3 seconds.
What you will learn
- repro.js: make the kitchen screen's tick fail on purpose
- kitchen.js: keep each queue's this when the screen ticks
- kitchen.test.js: a test that would have caught it
More in Everyday traps
Build "this in callbacks" 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.