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