Skip to content

Commit cfe2f1e

Browse files
authored
Introduce abortSignal.throwIfAborted()
Tests: web-platform-tests/wpt#31947. Closes #927.
1 parent 982c7e2 commit cfe2f1e

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

dom.bs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,11 +1695,9 @@ controller.abort();</code></pre>
16951695

16961696
<pre><code class=lang-javascript>
16971697
function doAmazingness({signal}) {
1698-
if (signal.aborted) {
1699-
return Promise.reject(signal.reason);
1700-
}
1701-
17021698
return new Promise((resolve, reject) => {
1699+
signal.throwIfAborted();
1700+
17031701
// Begin doing amazingness, and call resolve(result) when done.
17041702
// But also, watch for signals:
17051703
signal.addEventListener('abort', () => {
@@ -1776,6 +1774,7 @@ interface AbortSignal : EventTarget {
17761774

17771775
readonly attribute boolean aborted;
17781776
readonly attribute any reason;
1777+
undefined throwIfAborted();
17791778

17801779
attribute EventHandler onabort;
17811780
};</pre>
@@ -1786,11 +1785,14 @@ interface AbortSignal : EventTarget {
17861785
<var>reason</var> if not undefined; otherwise to an "{{AbortError!!exception}}" {{DOMException}}.
17871786

17881787
<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.
17911789

17921790
<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.
17941796
</dl>
17951797

17961798
<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.
18401842
<p>The <dfn attribute for=AbortSignal>reason</dfn> getter steps are to return <a>this</a>'s
18411843
<a for=AbortSignal>abort reason</a>.
18421844

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+
18431870
<p>The <dfn attribute for=AbortSignal><code>onabort</code></dfn> attribute is an
18441871
<a>event handler IDL attribute</a> for the <dfn export for=AbortSignal><code>onabort</code></dfn>
18451872
<a>event handler</a>, whose <a>event handler event type</a> is

0 commit comments

Comments
 (0)