Run the Routine Only Once

In Exercise: Add Snooze to the Routine, the Lindqvists’ routine learned to stop when someone asks it to. The opposite problem is that it can run more often than anyone wants. Erik and Saga both want their tea, and on a busy morning they tap “Good morning”, half a second apart. The routine should still run once.

In this lesson, you’ll write the guard most people write first and tap the button the way Erik and Saga do, with the second tap arriving while the routine is still running. You’ll trace what each tap does, line by line, and build a button that runs the routine once, however many times it’s tapped. Start with the button and a guard that looks right.

Tap the Button Twice

A button that two people can press has to remember whether the routine has already run, so a second tap doesn’t start every device again. A variable at the top of a module can hold that memory. Every call to the button’s function can read and change the same variable, the way functions reach the variables around them.

Work in your morning folder. Your devices.mjs should be the version from Make Your Own Functions Cancellable, where the kettle fills for two seconds, logs that it’s heating, and boils for four. If you’re not sure which version you have, then replace it with this one:

Language: JavaScript Filename: devices.mjs
import { setTimeout as delay } from "node:timers/promises";

export async function turnOnLights({ signal } = {}) {
  await delay(1000, undefined, { signal });
  return "Lights on";
}

export async function openBlinds({ signal } = {}) {
  await delay(2000, undefined, { signal });
  throw new Error("Blinds offline");
}

export async function warmUpHeating({ signal } = {}) {
  await delay(3000, undefined, { signal });
  return "Heating on";
}

async function fillKettle() {
  await delay(2000);
}

export async function boilKettle({ signal } = {}) {
  signal?.throwIfAborted();
  await fillKettle();
  signal?.throwIfAborted();
  console.log("Kettle heating");
  await delay(4000, undefined, { signal });
  console.log("Kettle clicks off");
  return "Kettle boiled";
}

The kettle’s two log lines are what you’ll watch in this lesson. Every time the routine runs, the terminal shows Kettle heating once and Kettle clicks off once. Your routine.mjs from the same lesson stays as it was too, starting all four devices and waiting for every one of them to settle:

Language: JavaScript Filename: routine.mjs
import {
  turnOnLights,
  openBlinds,
  warmUpHeating,
  boilKettle,
} from "./devices.mjs";

export async function runRoutine({ signal } = {}) {
  return Promise.allSettled([
    turnOnLights({ signal }),
    boilKettle({ signal }),
    openBlinds({ signal }),
    warmUpHeating({ signal }),
  ]);
}

Now save the button as button.mjs. It marks the morning done once the routine has finished, so a tap later in the morning doesn’t boil the kettle again:

Language: JavaScript Filename: button.mjs
import { runRoutine } from "./routine.mjs";

let morningDone = false;

export async function pressGoodMorning() {
  if (morningDone) {
    return "Already done this morning";
  }
  await runRoutine();
  morningDone = true;
  return "Routine finished";
}

Setting morningDone after the await seems sensible, since the morning isn’t done until the routine is. The button passes no signal, so there’s no Snooze in this lesson, and the routine takes its full six seconds.

To tap the button the way Erik and Saga do, save this script as two_taps.mjs. Its tap() function waits until the moment a person taps, presses the button, and logs what that person sees:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.