Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Watch Snooze Stop the Routine

In this video, you watch Saga press Snooze while the Lindqvists’ morning routine is running. In the last lesson, a signal from AbortSignal.timeout() aborted by itself after five seconds. This time, a person decides when to stop, with an AbortController. Snooze calls off whatever’s still running, and nothing starts again in this video.

This morning the blinds work. The delays are a scaled-down toy. The lights take one second, the blinds two, the heating three, and the kettle six.

Key Points

  • An AbortController decides when to stop. It’s a global that Node provides from the web standards, and you create one with new AbortController(). It holds a signal, snooze.signal, the same kind of AbortSignal you met with the deadline. Calling snooze.abort() aborts that signal.
  • The signal announces the abort. An AbortSignal is an event target, so aborting it fires one "abort" event to everything listening.
  • Only work that holds the signal stops. The timers for the lights, blinds and heating each got the signal in their options. When Saga pressed Snooze at 1.5 seconds, the lights had already finished, and an abort can’t undo work that’s done. Meanwhile, the timers for the blinds and heating heard the event, cleared themselves, and rejected with an AbortError.
  • Work without the signal keeps running. Nobody handed boilKettle() the signal, so the kettle kept boiling and clicked off at six seconds. Because Promise.allSettled() waits for every device, the report only printed at six seconds too.
  • An AbortError means the work was called off. The report names the snoozed devices AbortError, so the routine can tell a snoozed device from a failed one.

The Code

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

00:00 In the last lesson, the kettle’s timer held a signal that aborted by itself after five seconds. This time, a person decides when to stop. Erik wants five more minutes in bed, so Saga presses Snooze.

00:15 Here, Snooze calls off whatever’s still running, and nothing starts again. In this video, you’ll watch one Snooze press reach several devices at once, and see which ones stop.

00:28 Snooze is an AbortController, a global that Node provides from the web standards. You create it with the new keyword. Every controller holds a signal, the same kind of AbortSignal you met with the deadline.

00:40 When you call the controller’s abort() method, its signal aborts. The signal is an event target, so aborting it fires one abort event to everything listening.

00:53 Here, the global callback setTimeout(), not the delay() imported at the top, stands in for Saga’s finger. After one and a half seconds, it logs that Snooze was pressed and calls abort() on the controller.

01:08 The routine starts four devices together and waits for them all with Promise.allSettled(). Your device functions don’t take a signal yet. So for the lights, the blinds and the heating, the routine calls their timers directly, the way it called the kettle’s last time.

01:23 Each gets the same signal in its options.

01:27 This morning, the blinds work. The kettle stays boilKettle(), so you can see what happens to work that never gets the signal. These delays are a scaled-down toy, since a real kettle takes minutes.

01:43 Run it, and all four devices start at zero. The lights finish at one second. At one and a half seconds, the terminal shows Snooze pressed. The signal fires its abort event.

01:56 The timers for the blinds and the heating hear it, clear themselves, and reject with an abort error. Your turn: pause the video and predict what the kettle does now.

02:11 The kettle keeps boiling.

02:14 At six seconds, it clicks off. Nobody handed boilKettle() the signal, so the abort event never reached its timer. And because Promise.allSettled() waits for every device, the report only prints at six seconds too.

02:30 The lights had finished before the press, and an abort can’t undo work that’s already done. The report shows Lights on and Kettle boiled, then two abort errors.

02:41 You met that name in the last lesson. It marks work that was called off, so the routine can tell a snoozed device from a broken one. So here’s how the three parts work together.

02:53 The controller is the button, and calling abort() is the press. The signal carries the news, as one abort event. The work stops only if it holds the signal and listens.

03:06 To snooze the kettle too, boilKettle() needs a signal of its own to pass to its timer. Next, in Make Your Own Functions Cancellable, you’ll give your device functions a signal and make them listen.

Become a Member to join the conversation.