In this video, the Lindqvists’ blinds go offline, and you watch four promises settle side by side. A promise settles when it’s fulfilled or rejected. Promise.all() and three related methods each settle at their own moment, with their own result, while every device runs to its end.
Key Points
Promise.all()rejects at the first rejection, with that promise’s error, or fulfills once every promise fulfills. When the blinds reject at two seconds, it rejects at two seconds withBlinds offline.Promise.race()settles the way the first promise to settle does, fulfilled or rejected. With all four devices, the lights win at one second. Without the lights, the blinds settle first, so it rejects at two seconds.Promise.any()settles with the first promise to fulfill, if one does, and skips rejections on the way. Without the lights, it skips the blinds and fulfills at three seconds withHeating on. The next lesson, Report Which Devices Worked, shows what happens when none does.Promise.allSettled()waits until every promise has settled, fulfilled or rejected, and doesn’t reject because a device failed. It fulfills at six seconds, after the kettle, with an array of four results in the order you listed the devices. Each result has a status, and the blinds’ result holds their error.- No method stops a device. After
Promise.all()rejects at two seconds, the heating still turns on at three seconds and the kettle still boils at six. Nothing tells them to stop.
The Code
In devices.mjs, the blinds now go offline. openBlinds() waits two seconds and throws an Error, which rejects its promise:
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);
return "Kettle boiled";
}
The script starts the four devices and watches each device’s promise and each method’s promise. The seconds() helper is new: it gives the time since the script started, to a tenth of a second.
The watch() function logs that time, a label and the value when a promise fulfills, or the error’s message when it rejects. It doesn’t await anything. It attaches its .then() and .catch() callbacks and returns right away. So the script sets up every watch before anything settles, and each callback logs the moment its promise settles:
watch_settle.mjs
import {
turnOnLights,
boilKettle,
openBlinds,
warmUpHeating,
} from "./devices.mjs";
const start = Date.now();
const seconds = () => ((Date.now() - start) / 1000).toFixed(1);
function watch(label, promise) {
promise
.then((value) => console.log(`${seconds()} s: ${label} fulfilled:`, value))
.catch((error) => console.log(`${seconds()} s: ${label} rejected: ${error.message}`));
}
const routine = [turnOnLights, boilKettle, openBlinds, warmUpHeating];
const running = routine.map((startDevice) => startDevice());
for (const device of running) {
watch("device", device);
}
watch("all()", Promise.all(running));
watch("race()", Promise.race(running));
watch("any()", Promise.any(running));
watch("allSettled()", Promise.allSettled(running));
Here’s what the script printed. Each line prints as its promise settles, and the blinds’ result holds their error with its stack trace:
1.0 s: device fulfilled: Lights on
1.0 s: race() fulfilled: Lights on
1.0 s: any() fulfilled: Lights on
2.0 s: device rejected: Blinds offline
2.0 s: all() rejected: Blinds offline
3.0 s: device fulfilled: Heating on
6.0 s: device fulfilled: Kettle boiled
6.0 s: allSettled() fulfilled: [
{ status: 'fulfilled', value: 'Lights on' },
{ status: 'fulfilled', value: 'Kettle boiled' },
{
status: 'rejected',
reason: Error: Blinds offline
at openBlinds (file:///tmp/morning/devices.mjs:10:9)
},
{ status: 'fulfilled', value: 'Heating on' }
]
Next, in Report Which Devices Worked, you’ll read the results of Promise.allSettled() and pair each device with its status.