Skip to content

Commit 448a6a2

Browse files
jasnelltargos
authored andcommitted
lib: implement AbortSignal.abort()
Refs: whatwg/dom#960 PR-URL: #37693 Backport-PR-URL: #38386 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 4cd9f39 commit 448a6a2

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
@@ -283,6 +283,7 @@ module.exports = {
283283
},
284284
globals: {
285285
AbortController: 'readable',
286+
AbortSignal: 'readable',
286287
Atomics: 'readable',
287288
BigInt: 'readable',
288289
BigInt64Array: 'readable',

doc/api/globals.md

+9
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ added: REPLACEME
6565
The `AbortSignal` is used to notify observers when the
6666
`abortController.abort()` method is called.
6767

68+
#### Static method: `AbortSignal.abort()`
69+
<!-- YAML
70+
added: REPLACEME
71+
-->
72+
73+
* Returns: {AbortSignal}
74+
75+
Returns a new already aborted `AbortSignal`.
76+
6877
#### Event: `'abort'`
6978
<!-- YAML
7079
added: REPLACEME

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
Object.defineProperties(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
@@ -68,3 +68,8 @@ const { Event } = require('internal/event_target');
6868
strictEqual(toString(ac), '[object AbortController]');
6969
strictEqual(toString(ac.signal), '[object AbortSignal]');
7070
}
71+
72+
{
73+
const signal = AbortSignal.abort();
74+
ok(signal.aborted);
75+
}

0 commit comments

Comments
 (0)