@@ -1773,6 +1773,7 @@ interface AbortSignal : EventTarget {
1773
1773
1774
1774
readonly attribute boolean aborted;
1775
1775
readonly attribute any reason;
1776
+ undefined throwIfAborted();
1776
1777
1777
1778
attribute EventHandler onabort;
1778
1779
};</pre>
@@ -1783,11 +1784,14 @@ interface AbortSignal : EventTarget {
1783
1784
<var> reason</var> if not undefined; otherwise to an "{{AbortError!!exception}} " {{DOMException}} .
1784
1785
1785
1786
<dt><code><var> signal</var> . <a attribute for=AbortSignal>aborted</a> </code>
1786
- <dd> Returns true if this {{AbortSignal}} 's {{AbortController}} has signaled to abort; otherwise
1787
- false.
1787
+ <dd> Returns true if <var> signal</var> 's {{AbortController}} has signaled to abort; otherwise false.
1788
1788
1789
1789
<dt><code><var> signal</var> . <a attribute for=AbortSignal>reason</a> </code>
1790
- <dd> Returns this {{AbortSignal}} 's <a for=AbortSignal>abort reason</a> .
1790
+ <dd> Returns <var> signal</var> 's <a for=AbortSignal>abort reason</a> .
1791
+
1792
+ <dt><code><var> signal</var> . <a method for=AbortSignal lt=throwIfAborted()>throwIfAborted</a> ()</code>
1793
+ <dd> Throws <var> signal</var> 's <a for=AbortSignal>abort reason</a>, if <var>signal</var>' s
1794
+ {{AbortController}} has signaled to abort; otherwise, does nothing.
1791
1795
</dl>
1792
1796
1793
1797
<p> An {{AbortSignal}} object has an associated <dfn export for=AbortSignal>abort reason</dfn> , which is a
@@ -1837,6 +1841,31 @@ is [=AbortSignal/aborted=]; otherwise false.
1837
1841
<p> The <dfn attribute for=AbortSignal>reason</dfn> getter steps are to return <a>this</a> 's
1838
1842
<a for=AbortSignal>abort reason</a> .
1839
1843
1844
+ <p> The <dfn method for=AbortSignal>throwIfAborted()</dfn> method steps are to throw <a>this</a> 's
1845
+ <a for=AbortSignal>abort reason</a> , if <a>this</a> is [=AbortSignal/aborted=] .
1846
+
1847
+ <div class=example id=example-throwifaborted>
1848
+ <p> This method is primarily useful for when functions accepting {{AbortSignal}} s want to throw (or
1849
+ return a rejected promise) at specific checkpoints, instead of passing along the {{AbortSignal}}
1850
+ to other methods. For example, the following function allows aborting in between each attempt to
1851
+ poll for a condition. This gives opportunities to abort the polling process, even though the
1852
+ actual asynchronous operation (i.e., <code class=lang-javascript> await func()</code> ) does not
1853
+ accept an {{AbortSignal}} .
1854
+
1855
+ <pre class=lang-javascript>
1856
+ async function waitForCondition(func, targetValue, { signal } = {}) {
1857
+ while (true) {
1858
+ signal?.throwIfAborted();
1859
+
1860
+ const result = await func();
1861
+ if (result === targetValue) {
1862
+ return;
1863
+ }
1864
+ }
1865
+ }
1866
+ </pre>
1867
+ </div>
1868
+
1840
1869
<p> The <dfn attribute for=AbortSignal><code>onabort</code></dfn> attribute is an
1841
1870
<a>event handler IDL attribute</a> for the <dfn export for=AbortSignal><code>onabort</code></dfn>
1842
1871
<a>event handler</a> , whose <a>event handler event type</a> is
0 commit comments