lib: add reason to AbortSignal · nodejs/node@675c210 · GitHub
Skip to content

Commit 675c210

Browse files
jasnelltargos
authored andcommitted
lib: add reason to AbortSignal
A new reason property was recently added to the AbortSignal spec. ```js const ac = new AbortController(); ac.abort(new Error('boom!')); console.loc(ac.signal.reason); // Error('boom!'); ``` Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #40807 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
1 parent 62171eb commit 675c210

3 files changed

Lines changed: 73 additions & 8 deletions

File tree

doc/api/globals.md

Lines changed: 30 additions & 2 deletions

lib/internal/abort_controller.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const {
3030
} = require('internal/errors');
3131

3232
const kAborted = Symbol('kAborted');
33+
const kReason = Symbol('kReason');
3334

3435
function customInspect(self, obj, depth, options) {
3536
if (depth < 0)
@@ -52,19 +53,34 @@ class AbortSignal extends EventTarget {
5253
throw new ERR_ILLEGAL_CONSTRUCTOR();
5354
}
5455

56+
/**
57+
* @type {boolean}
58+
*/
5559
get aborted() {
5660
validateAbortSignal(this);
5761
return !!this[kAborted];
5862
}
5963

64+
/**
65+
* @type {any}
66+
*/
67+
get reason() {
68+
validateAbortSignal(this);
69+
return this[kReason];
70+
}
71+
6072
[customInspectSymbol](depth, options) {
6173
return customInspect(this, {
6274
aborted: this.aborted
6375
}, depth, options);
6476
}
6577

66-
static abort() {
67-
return createAbortSignal(true);
78+
/**
79+
* @param {any} reason
80+
* @returns {AbortSignal}
81+
*/
82+
static abort(reason) {
83+
return createAbortSignal(true, reason);
6884
}
6985
}
7086

@@ -81,16 +97,18 @@ ObjectDefineProperty(AbortSignal.prototype, SymbolToStringTag, {
8197

8298
defineEventHandler(AbortSignal.prototype, 'abort');
8399

84-
function createAbortSignal(aborted = false) {
100+
function createAbortSignal(aborted = false, reason = undefined) {
85101
const signal = new EventTarget();
86102
ObjectSetPrototypeOf(signal, AbortSignal.prototype);
87103
signal[kAborted] = aborted;
104+
signal[kReason] = reason;
88105
return signal;
89106
}
90107

91-
function abortSignal(signal) {
108+
function abortSignal(signal, reason) {
92109
if (signal[kAborted]) return;
93110
signal[kAborted] = true;
111+
signal[kReason] = reason;
94112
const event = new Event('abort', {
95113
[kTrustEvent]: true
96114
});
@@ -112,14 +130,20 @@ class AbortController {
112130
this[kSignal] = createAbortSignal();
113131
}
114132

133+
/**
134+
* @type {AbortSignal}
135+
*/
115136
get signal() {
116137
validateAbortController(this);
117138
return this[kSignal];
118139
}
119140

120-
abort() {
141+
/**
142+
* @param {any} reason
143+
*/
144+
abort(reason) {
121145
validateAbortController(this);
122-
abortSignal(this[kSignal]);
146+
abortSignal(this[kSignal], reason);
123147
}
124148

125149
[customInspectSymbol](depth, options) {

test/parallel/test-abortcontroller.js

Lines changed: 13 additions & 0 deletions

0 commit comments

Comments
 (0)