diff options
-rw-r--r-- | .eslintrc.js | 1 | ||||
-rw-r--r-- | doc/api/globals.md | 9 | ||||
-rw-r--r-- | lib/internal/abort_controller.js | 8 | ||||
-rw-r--r-- | test/parallel/test-abortcontroller.js | 5 |
4 files changed, 21 insertions, 2 deletions
diff --git a/.eslintrc.js b/.eslintrc.js index 291436cdc4..7574e1b4b6 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -315,6 +315,7 @@ module.exports = { }, globals: { AbortController: 'readable', + AbortSignal: 'readable', Atomics: 'readable', BigInt: 'readable', BigInt64Array: 'readable', diff --git a/doc/api/globals.md b/doc/api/globals.md index be3c6d6ad9..7b8c7249f5 100644 --- a/doc/api/globals.md +++ b/doc/api/globals.md @@ -67,6 +67,15 @@ added: v15.0.0 The `AbortSignal` is used to notify observers when the `abortController.abort()` method is called. +#### Static method: `AbortSignal.abort()` +<!-- YAML +added: REPLACEME +--> + +* Returns: {AbortSignal} + +Returns a new already aborted `AbortSignal`. + #### Event: `'abort'` <!-- YAML added: v15.0.0 diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js index 21414cde0e..5a16a56a4a 100644 --- a/lib/internal/abort_controller.js +++ b/lib/internal/abort_controller.js @@ -50,6 +50,10 @@ class AbortSignal extends EventTarget { aborted: this.aborted }, depth, options); } + + static abort() { + return createAbortSignal(true); + } } ObjectDefineProperties(AbortSignal.prototype, { @@ -65,10 +69,10 @@ ObjectDefineProperty(AbortSignal.prototype, SymbolToStringTag, { defineEventHandler(AbortSignal.prototype, 'abort'); -function createAbortSignal() { +function createAbortSignal(aborted = false) { const signal = new EventTarget(); ObjectSetPrototypeOf(signal, AbortSignal.prototype); - signal[kAborted] = false; + signal[kAborted] = aborted; return signal; } diff --git a/test/parallel/test-abortcontroller.js b/test/parallel/test-abortcontroller.js index 283aaa93fc..2b36da332e 100644 --- a/test/parallel/test-abortcontroller.js +++ b/test/parallel/test-abortcontroller.js @@ -67,3 +67,8 @@ const { ok, strictEqual, throws } = require('assert'); strictEqual(toString(ac), '[object AbortController]'); strictEqual(toString(ac.signal), '[object AbortSignal]'); } + +{ + const signal = AbortSignal.abort(); + ok(signal.aborted); +} |