Your routine starts every device at once with .map() and waits for them with Promise.all(), and it works. But how much time did that save? In this video, you watch the Lindqvists’ morning routine run twice on one clock, once one device at a time and once with all four devices started together.
This morning the blinds work again, so every device succeeds. The delays are a scaled-down toy. The lights take one second, the blinds two, the heating three, and the kettle six.
Key Points
- One at a time, the routine takes about the sum of the delays. A
for...ofloop with oneawaitin its body is the same one-at-a-time shape as four awaits in a row, since it doesn’t call the next device until the one before it is done. Each device starts where the last one ended, like a staircase, so the routine takes 1 + 6 + 2 + 3 = 12 seconds. - All together, the routine takes only about the slowest delay.
.map()calls all four device functions without waiting for any of them, and each call hands back a pending promise straight away. TheawaitonPromise.all()then pauses your code until every promise is fulfilled. All four start at zero and finish in order of their delays, so the routine is done when the kettle boils, at six seconds. - The waits overlap because all four devices start before the
await. The.map()call starts all four timers before your code reaches theawait, so they count down in Node at the same time. - Taking out a fast device changes the sum, not the slowest. Without the lights, the rule predicts that one at a time drops from twelve seconds to eleven. All together still takes six, because the kettle is still the slowest device.
- Starting the four devices at once saves the Lindqvists six seconds.
- The timelines are idealized. A timer waits about its delay, so a real run comes out a little different from these round numbers, usually a little longer.
The Code
The stand-in devices, with the blinds working again:
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);
return "Blinds open";
}
export async function warmUpHeating() {
await delay(3000);
return "Heating on";
}
export async function boilKettle() {
await delay(6000);
return "Kettle boiled";
}
One device at a time, each await pauses the for...of loop until that device is done, so the next device isn’t called until then. The seconds() helper returns how long it’s been since the start:
one_at_a_time.mjs
import {
turnOnLights,
boilKettle,
openBlinds,
warmUpHeating,
} from "./devices.mjs";
const start = Date.now();
const seconds = () => ((Date.now() - start) / 1000).toFixed(1);
const routine = [turnOnLights, boilKettle, openBlinds, warmUpHeating];
for (const startDevice of routine) {
const status = await startDevice();
console.log(`${seconds()} s: ${status}`);
}
Run it, and the routine takes twelve seconds:
$ node one_at_a_time.mjs
1.0 s: Lights on
7.0 s: Kettle boiled
9.0 s: Blinds open
12.0 s: Heating on
All four devices at once, started by .map() and awaited with Promise.all(), with the same helper:
all_together.mjs
import {
turnOnLights,
boilKettle,
openBlinds,
warmUpHeating,
} from "./devices.mjs";
const start = Date.now();
const seconds = () => ((Date.now() - start) / 1000).toFixed(1);
const routine = [turnOnLights, boilKettle, openBlinds, warmUpHeating];
const running = routine.map((startDevice) => startDevice());
const statuses = await Promise.all(running);
console.log(`${seconds()} s:`, statuses);
Run it, and the routine takes six seconds, the kettle’s delay:
$ node all_together.mjs
6.0 s: [ 'Lights on', 'Kettle boiled', 'Blinds open', 'Heating on' ]
To try the prediction, take the lights out of routine, so the line reads const routine = [boilKettle, openBlinds, warmUpHeating];. One at a time, the routine should take 6 + 2 + 3 = 11 seconds. All together, it should still take six, the kettle’s delay.
Next, in Time the Routine Two Ways, you’ll run the routine without the lights and check the six and eleven seconds you predicted.