diff --git a/dom.bs b/dom.bs index 5e04fed6c..786ab7a33 100644 --- a/dom.bs +++ b/dom.bs @@ -1695,11 +1695,9 @@ controller.abort();

 function doAmazingness({signal}) {
-  if (signal.aborted) {
-    return Promise.reject(signal.reason);
-  }
-
   return new Promise((resolve, reject) => {
+    signal.throwIfAborted();
+
     // Begin doing amazingness, and call resolve(result) when done.
     // But also, watch for signals:
     signal.addEventListener('abort', () => {
@@ -1776,6 +1774,7 @@ interface AbortSignal : EventTarget {
 
   readonly attribute boolean aborted;
   readonly attribute any reason;
+  undefined throwIfAborted();
 
   attribute EventHandler onabort;
 };
@@ -1786,11 +1785,14 @@ interface AbortSignal : EventTarget { reason if not undefined; otherwise to an "{{AbortError!!exception}}" {{DOMException}}.
signal . aborted -
Returns true if this {{AbortSignal}}'s {{AbortController}} has signaled to abort; otherwise - false. +
Returns true if signal's {{AbortController}} has signaled to abort; otherwise false.
signal . reason -
Returns this {{AbortSignal}}'s abort reason. +
Returns signal's abort reason. + +
signal . throwIfAborted() +
Throws signal's abort reason, if signal's + {{AbortController}} has signaled to abort; otherwise, does nothing.

An {{AbortSignal}} object has an associated abort reason, which is a @@ -1840,6 +1842,31 @@ is [=AbortSignal/aborted=]; otherwise false.

The reason getter steps are to return this's abort reason. +

The throwIfAborted() method steps are to throw this's +abort reason, if this is [=AbortSignal/aborted=]. + +

+

This method is primarily useful for when functions accepting {{AbortSignal}}s want to throw (or + return a rejected promise) at specific checkpoints, instead of passing along the {{AbortSignal}} + to other methods. For example, the following function allows aborting in between each attempt to + poll for a condition. This gives opportunities to abort the polling process, even though the + actual asynchronous operation (i.e., await func()) does not + accept an {{AbortSignal}}. + +

+ async function waitForCondition(func, targetValue, { signal } = {}) {
+   while (true) {
+     signal?.throwIfAborted();
+
+     const result = await func();
+     if (result === targetValue) {
+       return;
+     }
+   }
+ }
+ 
+
+

The onabort attribute is an event handler IDL attribute for the onabort event handler, whose event handler event type is