Skip to content

Commit 8adf7d5

Browse files
jasnelltargos
authored andcommitted
lib: add abortSignal.throwIfAborted()
Refs: whatwg/dom#1034 Signed-off-by: James M Snell <[email protected]> PR-URL: #40951 Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 1bd5816 commit 8adf7d5

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

‎doc/api/globals.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ ac.abort(new Error('boom!'));
179179
console.log(ac.signal.reason); // Error('boom!');
180180
```
181181

182+
#### `abortSignal.throwIfAborted()`
183+
184+
<!-- YAML
185+
added: REPLACEME
186+
-->
187+
188+
If `abortSignal.aborted` is `true`, throws `abortSignal.reason`.
189+
182190
## Class: `Buffer`
183191

184192
<!-- YAML

‎lib/internal/abort_controller.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ class AbortSignal extends EventTarget{
141141
returnthis[kReason];
142142
}
143143

144+
throwIfAborted(){
145+
if(this.aborted){
146+
throwthis.reason;
147+
}
148+
}
149+
144150
[customInspectSymbol](depth,options){
145151
returncustomInspect(this,{
146152
aborted: this.aborted
@@ -151,7 +157,8 @@ class AbortSignal extends EventTarget{
151157
* @param{any} reason
152158
* @returns{AbortSignal}
153159
*/
154-
staticabort(reason){
160+
staticabort(
161+
reason=newDOMException('This operation was aborted','AbortError')){
155162
returncreateAbortSignal(true,reason);
156163
}
157164

@@ -311,7 +318,7 @@ class AbortController{
311318
/**
312319
* @param{any} reason
313320
*/
314-
abort(reason){
321+
abort(reason=newDOMException('This operation was aborted','AbortError')){
315322
validateAbortController(this);
316323
abortSignal(this[kSignal],reason);
317324
}

‎test/parallel/test-abortcontroller.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const{
1414
const{
1515
kWeakHandler,
1616
}=require('internal/event_target');
17+
const{ internalBinding }=require('internal/test/binding');
18+
const{ DOMException }=internalBinding('messaging');
1719

1820
const{setTimeout: sleep}=require('timers/promises');
1921

@@ -230,3 +232,24 @@ const{setTimeout: sleep } = require('timers/promises');
230232
// keep the Node.js process open (the timer is unref'd)
231233
AbortSignal.timeout(1_200_000);
232234
}
235+
236+
{
237+
// Test AbortSignal.reason default
238+
constsignal=AbortSignal.abort();
239+
ok(signal.reasoninstanceofDOMException);
240+
strictEqual(signal.reason.code,20);
241+
242+
constac=newAbortController();
243+
ac.abort();
244+
ok(ac.signal.reasoninstanceofDOMException);
245+
strictEqual(ac.signal.reason.code,20);
246+
}
247+
248+
{
249+
// Test abortSignal.throwIfAborted()
250+
throws(()=>AbortSignal.abort().throwIfAborted(),{code: 20});
251+
252+
// Does not throw because it's not aborted.
253+
constac=newAbortController();
254+
ac.signal.throwIfAborted();
255+
}

0 commit comments

Comments
(0)