Catch Where You Can Respond

At the end of Give Every Promise an Owner, the blinds’ failure reached a catch block at the top of good_morning.mjs. The routine printed Routine failed: Blinds offline, and then echo $? printed 0. The program said the routine had failed, but its exit code said it had worked.

A job that starts the routine every morning reads only that number, so it would never know. In this lesson, you’ll decide which part of the routine should catch a device’s failure, add what that part knows to the error before passing it on, and end the program with a clear message and a non-zero exit code. Start by splitting the routine into the levels a failure travels through.

Find the Level That Can Respond

A failure in one device passes through every function between the device and the person who tapped the button. To decide where to catch it, you first need to see those functions as separate levels. Keep devices.mjs as it was in the last lesson, with the blinds offline after two seconds:

Language: JavaScript Filename: 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";
}

Next, give the routine its own module. Save this as routine.mjs. Its runRoutine() function starts all four devices and returns the promise from Promise.all(), so the caller owns it:

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

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

There’s no try block in runRoutine(), so a plain return is safe here, as you saw in the last lesson. Now replace the contents of good_morning.mjs with the button handler. In the finished app, tapping Good morning would call pressGoodMorning(). Here, the last line taps it once:

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

async function pressGoodMorning() {
  try {
    const statuses = await runRoutine();
    console.log(statuses.join(", "));
  } catch (error) {
    console.log(`Routine failed: ${error.message}`);
  }
}

await pressGoodMorning();

Run it, and check the exit code again. The message appears after two seconds, but the prompt comes back only after six:

Language: Shell
$ node good_morning.mjs
Routine failed: Blinds offline
$ echo $?
0

The program waited for the kettle anyway. Promise.all() gave up at two seconds, but it doesn’t stop those devices, and Node.js keeps running until their timers finish. The program still ended with 0 because a catch block that prints a message counts as handling the error. As far as Node.js can tell, nothing went wrong.

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.