Skip to content

Commit ac2f50b

Browse files
jasnelldanielleadams
authored andcommitted
lib: implement AbortSignal.abort()
Refs: whatwg/dom#960 PR-URL: #37693 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 0d135e8 commit ac2f50b

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ module.exports = {
315315
},
316316
globals: {
317317
AbortController: 'readable',
318+
AbortSignal: 'readable',
318319
Atomics: 'readable',
319320
BigInt: 'readable',
320321
BigInt64Array: 'readable',

doc/api/globals.md

+9
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ added: v15.0.0
6767
The `AbortSignal` is used to notify observers when the
6868
`abortController.abort()` method is called.
6969

70+
#### Static method: `AbortSignal.abort()`
71+
<!-- YAML
72+
added: REPLACEME
73+
-->
74+
75+
* Returns: {AbortSignal}
76+
77+
Returns a new already aborted `AbortSignal`.
78+
7079
#### Event: `'abort'`
7180
<!-- YAML
7281
added: v15.0.0

lib/internal/abort_controller.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ class AbortSignal extends EventTarget {
5050
aborted: this.aborted
5151
}, depth, options);
5252
}
53+
54+
static abort() {
55+
return createAbortSignal(true);
56+
}
5357
}
5458

5559
ObjectDefineProperties(AbortSignal.prototype, {
@@ -65,10 +69,10 @@ ObjectDefineProperty(AbortSignal.prototype, SymbolToStringTag, {
6569

6670
defineEventHandler(AbortSignal.prototype, 'abort');
6771

68-
function createAbortSignal() {
72+
function createAbortSignal(aborted = false) {
6973
const signal = new EventTarget();
7074
ObjectSetPrototypeOf(signal, AbortSignal.prototype);
71-
signal[kAborted] = false;
75+
signal[kAborted] = aborted;
7276
return signal;
7377
}
7478

test/parallel/test-abortcontroller.js

+5
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,8 @@ const { ok, strictEqual, throws } = require('assert');
6767
strictEqual(toString(ac), '[object AbortController]');
6868
strictEqual(toString(ac.signal), '[object AbortSignal]');
6969
}
70+
71+
{
72+
const signal = AbortSignal.abort();
73+
ok(signal.aborted);
74+
}

0 commit comments

Comments
 (0)