In the last lesson, your routine started every device together and really waited for all four. Now Erik and Saga have put smart bulbs in every room, twelve lights in all, and their home hub can pass on only about four commands at a time. Send it more, and some lights miss their command.
In this lesson, you’ll start the twelve lights a few at a time, so the routine stays fast without swamping the hub, and you’ll see how the batch size sets the total time. Start by sending commands to all twelve at once and watching some lights stay off.
Start Twelve Lights a Few at a Time
In your morning folder, save the stand-in for the lights as lights.mjs. As with turnOnLights() in devices.mjs, turning on each light takes a second. The busy counter is a toy hub, which misses any command that arrives while four are already in flight:
lights.mjs
import { setTimeout as delay } from "node:timers/promises";
export const rooms = [
"Porch", "Hall", "Stairs", "Landing",
"Bathroom", "Main bedroom", "Erik's desk", "Saga's desk",
"Guest room", "Closet", "Kitchen", "Pantry",
];
let busy = 0;
export async function turnOnLight(room) {
busy += 1;
const missed = busy > 4;
await delay(1000);
busy -= 1;
return missed ? `${room} missed (hub busy)` : `${room} on`;
}
Then start every light the way you started the four devices, in all_lights.mjs:
all_lights.mjs
import { rooms, turnOnLight } from "./lights.mjs";
const statuses = await Promise.all(
rooms.map((room) => turnOnLight(room)),
);
console.log(statuses.join("\n"));