| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 70e6fe8 commit 4ba883d
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -191,6 +191,14 @@ ac.abort(new Error('boom!')); | |||
| 191 | 191 | console.log(ac.signal.reason); // Error('boom!'); | |
| 192 | 192 | ``` | |
| 193 | 193 | ||
| 194 | + #### `abortSignal.throwIfAborted()` | ||
| 195 | + | ||
| 196 | + <!-- YAML | ||
| 197 | + added: REPLACEME | ||
| 198 | + --> | ||
| 199 | + | ||
| 200 | + If `abortSignal.aborted` is `true`, throws `abortSignal.reason`. | ||
| 201 | + | ||
| 194 | 202 | ## Class: `Buffer` | |
| 195 | 203 | ||
| 196 | 204 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -116,6 +116,12 @@ class AbortSignal extends EventTarget { | |||
| 116 | 116 | return this[kReason]; | |
| 117 | 117 | } | |
| 118 | 118 | ||
| 119 | + throwIfAborted() { | ||
| 120 | + if (this.aborted) { | ||
| 121 | + throw this.reason; | ||
| 122 | + } | ||
| 123 | + } | ||
| 124 | + | ||
| 119 | 125 | [customInspectSymbol](depth, options) { | |
| 120 | 126 | return customInspect(this, { | |
| 121 | 127 | aborted: this.aborted | |
@@ -126,7 +132,8 @@ class AbortSignal extends EventTarget { | |||
| 126 | 132 | * @param {any} reason | |
| 127 | 133 | * @returns {AbortSignal} | |
| 128 | 134 | */ | |
| 129 | - static abort(reason) { | ||
| 135 | + static abort( | ||
| 136 | + reason = new DOMException('This operation was aborted', 'AbortError')) { | ||
| 130 | 137 | return createAbortSignal(true, reason); | |
| 131 | 138 | } | |
| 132 | 139 | ||
@@ -224,7 +231,7 @@ class AbortController { | |||
| 224 | 231 | /** | |
| 225 | 232 | * @param {any} reason | |
| 226 | 233 | */ | |
| 227 | - abort(reason) { | ||
| 234 | + abort(reason = new DOMException('This operation was aborted', 'AbortError')) { | ||
| 228 | 235 | validateAbortController(this); | |
| 229 | 236 | abortSignal(this[kSignal], reason); | |
| 230 | 237 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -230,3 +230,24 @@ const { setTimeout: sleep } = require('timers/promises'); | |||
| 230 | 230 | // keep the Node.js process open (the timer is unref'd) | |
| 231 | 231 | AbortSignal.timeout(1_200_000); | |
| 232 | 232 | } | |
| 233 | + | ||
| 234 | + { | ||
| 235 | + // Test AbortSignal.reason default | ||
| 236 | + const signal = AbortSignal.abort(); | ||
| 237 | + ok(signal.reason instanceof DOMException); | ||
| 238 | + strictEqual(signal.reason.code, 20); | ||
| 239 | + | ||
| 240 | + const ac = new AbortController(); | ||
| 241 | + ac.abort(); | ||
| 242 | + ok(ac.signal.reason instanceof DOMException); | ||
| 243 | + strictEqual(ac.signal.reason.code, 20); | ||
| 244 | + } | ||
| 245 | + | ||
| 246 | + { | ||
| 247 | + // Test abortSignal.throwIfAborted() | ||
| 248 | + throws(() => AbortSignal.abort().throwIfAborted(), { code: 20 }); | ||
| 249 | + | ||
| 250 | + // Does not throw because it's not aborted. | ||
| 251 | + const ac = new AbortController(); | ||
| 252 | + ac.signal.throwIfAborted(); | ||
| 253 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments