In Exercise: Report a Failed Device, each device failure reached the level that could report it. The kettle has a different problem. It never fails, but it’s slow, and Erik doesn’t want the whole routine to wait six seconds for it.
So the routine gets a rule: if the kettle isn’t done after five seconds, give up on it. In this lesson, you’ll build that deadline the way most people try first, run it to see what it does, and then give the kettle a deadline that really stops its wait. You’ll also tell a deadline apart from a real failure. Start with a race, a tool you already have.
Race the Kettle Against a Deadline
To give up on the kettle, the routine needs something that settles after five seconds and a way to stop waiting when it does. In Watch Four Promises Settle, you saw that Promise.race() takes on the outcome of the first promise to settle. So race the kettle against a promise that rejects after five seconds.
First, give the kettle a way to tell you when it finishes, even when nobody awaits its result. A real kettle clicks off when the water boils. In devices.mjs, add a line to boilKettle() that logs that click, and keep the rest of the file as it was:
devices.mjs
import { setTimeout as delay } from "node:timers/promises";
export async function turnOnLights() {
await delay(1000);
return "Lights on";
}
export async function openBlinds() {
await delay(2000);
throw new Error("Blinds offline");
}
export async function warmUpHeating() {
await delay(3000);
return "Heating on";
}
export async function boilKettle() {
await delay(6000);
console.log("Kettle clicks off");
return "Kettle boiled";
}
Now save the deadline as kettle_race.mjs. Its deadline() function waits for the duration you give it and then throws, so its promise rejects. The try block races the kettle against a five-second deadline:
kettle_race.mjs
import { setTimeout as delay } from "node:timers/promises";
import { boilKettle } from "./devices.mjs";
const start = Date.now();
const seconds = () => ((Date.now() - start) / 1000).toFixed(1);
async function deadline(ms) {
await delay(ms);
throw new Error(`No answer after ${ms / 1000} s`);
}
try {
const status = await Promise.race([
boilKettle(),
deadline(5000),
]);
console.log(`${seconds()} s: ${status}`);
} catch (error) {
console.log(`${seconds()} s: Kettle skipped: ${error.message}`);
}
console.log(`${seconds()} s: Routine moves on`);
The deadline wins the race, since five seconds is less than six. Before you run node kettle_race.mjs, decide what the terminal will show and when the prompt will come back.
Two lines appear after five seconds, and one more a second later:
$ node kettle_race.mjs
5.0 s: Kettle skipped: No answer after 5 s
5.0 s: Routine moves on
Kettle clicks off
The routine gave up on the kettle at five seconds, as planned. Then, at six seconds, the kettle clicked off anyway. The routine told Erik there’d be no tea, and the water boiled with nobody waiting for it.
The kettle kept boiling because Promise.race() only decides what your code waits for. When the deadline rejected, the race’s promise rejected too, and the await stopped waiting. But nothing told the kettle. A promise is a report on work that’s already running. It has no way to reach back into that work and stop it.
The kettle’s six-second timer was still counting down in Node.js. When it was due, the event loop moved its task onto the empty call stack. That task fulfilled the promise from delay(), and boilKettle() carried on after its await as a microtask, clicked, and returned a status that nobody read.
That’s the same rule you saw when the blinds went offline: no combinator stops a device. A deadline built with Promise.race() still uses a combinator, so it stops your waiting and leaves the work running.
It works the other way round too. To see it, give the kettle a longer deadline than it needs. In kettle_race.mjs, change deadline(5000) to deadline(7000):
kettle_race.mjs
import { setTimeout as delay } from "node:timers/promises";
import { boilKettle } from "./devices.mjs";
const start = Date.now();
const seconds = () => ((Date.now() - start) / 1000).toFixed(1);
async function deadline(ms) {
await delay(ms);
throw new Error(`No answer after ${ms / 1000} s`);
}
try {
const status = await Promise.race([
boilKettle(),
deadline(7000),
]);
console.log(`${seconds()} s: ${status}`);
} catch (error) {
console.log(`${seconds()} s: Kettle skipped: ${error.message}`);
}
console.log(`${seconds()} s: Routine moves on`);
Run it again. All three lines appear after six seconds, but the prompt only comes back at seven:
$ node kettle_race.mjs
Kettle clicks off
6.0 s: Kettle boiled
6.0 s: Routine moves on
This time the kettle won the race, and the deadline’s timer kept running until it was due, so Node.js stayed open for that last second. The deadline’s rejection didn’t stop the program, because Promise.race() had attached a handler to its promise. The race leaves the loser running, whichever one loses.
If you’d like to check that you can explain this in your own words, then ask the mentor. This button opens a chat with the question already typed and a pointer to this section: