How Node really works · Chapter 4 of 34
Timers
By the end of this chapter you can schedule work for later, cancel it, keep it from holding the process open, and await it with a way to call it off. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: How Node runs
- 5 tasks
- Node.js
- In the all labs plan
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.
Why you need this
Ravi wants the 4 pm rush on autopilot: a reminder to the rider ten minutes after an order is packed (unless he has picked it up), a boiler check every five minutes for a shift, and a "still alive" ping to the monitoring service while the order server runs. The kitchen wants a brewing timer a customer can cancel, and Kabir wants a time limit on calls to slow services.
You know setTimeout from the browser, but a server runs for days, and a timer nobody stops shows up later: a reminder sent after the pickup, a check that runs all night, a server that will not shut down because a ping is still scheduled, thousands of forgotten timeouts.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
remindRider: remind a rider, unless he picks the order up first
Write remindRider(rider, ms, send). After ms milliseconds it calls send once, with the message <rider>, your order is ready (for Ravi: Ravi, your order is ready). It returns a cancel function.
cancel() stops the reminder and returns true if the reminder had not been sent yet. It returns false if the reminder was already sent or already cancelled, so two calls before the reminder is due give true, then false. A cancelled reminder leaves no timer behind.
- Task 2
startBoilerCheck: check the boiler, then stop after the last check
Write startBoilerCheck(check, everyMs, times). It calls check(1), check(2) and so on up to check(times), one call every everyMs milliseconds, the first after everyMs, and then clears its own interval. It returns a stop function that ends the checks early.
After the last check, or after stop is called, no timer may be left running, and check is never called again.
- Task 3
startHeartbeat: a heartbeat that never keeps the server up
Write startHeartbeat(beat, ms). It calls beat every ms milliseconds and returns the interval's own handle (the Timeout object, not a wrapper), so the caller can stop it with clearInterval.
The heartbeat must never keep the process alive: in plain Node, a script that has only the heartbeat left exits by itself, and the handle's hasRef() is false.
- Task 4
prepTimer: a brewing timer that can be called off
Write prepTimer(ms, signal), an async function; signal is an optional AbortSignal. It waits ms milliseconds and resolves to 'ready'.
If signal aborts first, it resolves to 'cancelled' (it does not reject), and no timer is left running. A signal that is already aborted when prepTimer is called gives 'cancelled' straight away.
- Task 5 · interview question
Interview: withTimeout, a time limit on any promise that clears its timer
Write withTimeout(promise, ms). It returns a promise that settles exactly like promise, with the same value or the same error, unless ms milliseconds pass first: then it rejects with an Error whose message is Timed out after <ms> ms (Timed out after 20 ms for ms = 20).
Whichever happens first, no timer may be left running afterwards: not when the promise resolves in time, not when it rejects in time, and not after the time-out.
What you will learn
- Handles and clearing: every timer returns a handle, and the handle is how you stop it.
- ref and unref: which timers keep the process alive.
- Timers as promises: await a delay, and call it off with an AbortSignal.
- The leaking timeout: why a race against a timer must clean up.
More in How Node runs
Build "Timers" yourself
Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.