From e08df49d7ad8ea21f12033144becfa986989a9bc Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sat, 13 Nov 2021 08:27:32 -0800 Subject: [PATCH] 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 PR-URL: https://github.com/nodejs/node/pull/40807 Reviewed-By: Antoine du Hamel Reviewed-By: Benjamin Gruenbaum Reviewed-By: Luigi Pinca Reviewed-By: Minwoo Jung Reviewed-By: Robert Nagy --- doc/api/globals.md | 40 ++++++++++++++++++++++++--- lib/internal/abort_controller.js | 36 ++++++++++++++++++++---- test/parallel/test-abortcontroller.js | 13 +++++++++ 3 files changed, 79 insertions(+), 10 deletions(-) diff --git a/doc/api/globals.md b/doc/api/globals.md index c7e539ca41aade..c81a49ab3ff09d 100644 --- a/doc/api/globals.md +++ b/doc/api/globals.md @@ -44,12 +44,21 @@ ac.abort(); console.log(ac.signal.aborted); // Prints True ``` -### `abortController.abort()` +### `abortController.abort([reason])` +* `reason` {any} An optional reason, retrievable on the `AbortSignal`s + `reason` property. + Triggers the abort signal, causing the `abortController.signal` to emit the `'abort'` event. @@ -72,12 +81,19 @@ added: v15.0.0 The `AbortSignal` is used to notify observers when the `abortController.abort()` method is called. -#### Static method: `AbortSignal.abort()` +#### Static method: `AbortSignal.abort([reason])` +* `reason`: {any} * Returns: {AbortSignal} Returns a new already aborted `AbortSignal`. @@ -136,6 +152,22 @@ added: v15.0.0 An optional callback function that may be set by user code to be notified when the `abortController.abort()` function has been called. +#### `abortSignal.reason` + + + +* Type: {any} + +An optional reason specified when the `AbortSignal` was triggered. + +```js +const ac = new AbortController(); +ac.abort(new Error('boom!')); +console.log(ac.signal.reason); // Error('boom!'); +``` + ## Class: `Buffer`