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
AbortControllerdecides when to stop. It’s a global that Node provides from the web standards, and you create one withnew AbortController(). It holds a signal,snooze.signal, the same kind ofAbortSignalyou met with the deadline. Callingsnooze.abort()aborts that signal. - The signal announces the abort. An
AbortSignalis 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. BecausePromise.allSettled()waits for every device, the report only printed at six seconds too. - An
AbortErrormeans the work was called off. The report names the snoozed devicesAbortError, so the routine can tell a snoozed device from a failed one.