Skip to content

Commit d001912

Browse files
committed
Introduce abortSignal.throwIfAborted()
Closes #927.
1 parent 4ca187f commit d001912

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

dom.bs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,7 @@ interface AbortSignal : EventTarget {
17731773

17741774
readonly attribute boolean aborted;
17751775
readonly attribute any reason;
1776+
undefined throwIfAborted();
17761777

17771778
attribute EventHandler onabort;
17781779
};</pre>
@@ -1783,11 +1784,14 @@ interface AbortSignal : EventTarget {
17831784
<var>reason</var> if not undefined; otherwise to an "{{AbortError!!exception}}" {{DOMException}}.
17841785

17851786
<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.
17881788

17891789
<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.
17911795
</dl>
17921796

17931797
<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.
18371841
<p>The <dfn attribute for=AbortSignal>reason</dfn> getter steps are to return <a>this</a>'s
18381842
<a for=AbortSignal>abort reason</a>.
18391843

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

0 commit comments

Comments
 (0)