From 675c210b0424eced29a45eebfd5a8fc50ee9d2bb 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 | 32 ++++++++++++++++++++++-- lib/internal/abort_controller.js | 36 ++++++++++++++++++++++----- test/parallel/test-abortcontroller.js | 13 ++++++++++ 3 files changed, 73 insertions(+), 8 deletions(-) diff --git a/doc/api/globals.md b/doc/api/globals.md index 8cc52e3a01433f..88e9aad9df21f8 100644 --- a/doc/api/globals.md +++ b/doc/api/globals.md @@ -46,14 +46,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. @@ -80,14 +87,19 @@ added: 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`. @@ -152,6 +164,22 @@ added: 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`