@@ -2123,39 +2123,55 @@ added: v19.7.0
21232123> Stability: 1 - Experimental
21242124
21252125* ` signal` {AbortSignal}
2126- * ` resource` {Object} Any non-null entity, reference to which is held weakly.
2126+ * ` resource` {Object} Any non-null object tied to the abortable operation and held weakly.
2127+ If ` resource` is garbage collected before the ` signal` aborts, the promise remains pending,
2128+ allowing Node.js to stop tracking it.
2129+ This helps prevent memory leaks in long-running or non-cancelable operations.
21272130* Returns: {Promise}
21282131
2129- Listens to abort event on the provided ` signal` and
2130- returns a promise that is fulfilled when the ` signal` is
2131- aborted. If the passed ` resource` is garbage collected before the ` signal` is
2132- aborted, the returned promise shall remain pending indefinitely.
2132+ Listens to abort event on the provided ` signal` and returns a promise that resolves when the ` signal` is aborted.
2133+ If ` resource` is provided, it weakly references the operation's associated object,
2134+ so if ` resource` is garbage collected before the ` signal` aborts,
2135+ then returned promise shall remain pending.
2136+ This prevents memory leaks in long-running or non-cancelable operations.
21332137
21342138` ` ` cjs
21352139const { aborted } = require (' node:util' );
21362140
2141+ // Obtain an object with an abortable signal, like a custom resource or operation.
21372142const dependent = obtainSomethingAbortable ();
21382143
2144+ // Pass `dependent` as the resource, indicating the promise should only resolve
2145+ // if `dependent` is still in memory when the signal is aborted.
21392146aborted (dependent .signal , dependent).then (() => {
2140- // Do something when dependent is aborted.
2147+
2148+ // This code runs when `dependent` is aborted.
2149+ console .log (' Dependent resource was aborted.' );
21412150});
21422151
2152+ // Simulate an event that triggers the abort.
21432153dependent .on (' event' , () => {
2144- dependent .abort ();
2154+ dependent .abort (); // This will cause the `aborted` promise to resolve.
21452155});
21462156` ` `
21472157
21482158` ` ` mjs
21492159import { aborted } from ' node:util' ;
21502160
2161+ // Obtain an object with an abortable signal, like a custom resource or operation.
21512162const dependent = obtainSomethingAbortable ();
21522163
2164+ // Pass `dependent` as the resource, indicating the promise should only resolve
2165+ // if `dependent` is still in memory when the signal is aborted.
21532166aborted (dependent .signal , dependent).then (() => {
2154- // Do something when dependent is aborted.
2167+
2168+ // This code runs when `dependent` is aborted.
2169+ console .log (' Dependent resource was aborted.' );
21552170});
21562171
2172+ // Simulate an event that triggers the abort.
21572173dependent .on (' event' , () => {
2158- dependent .abort ();
2174+ dependent .abort (); // This will cause the `aborted` promise to resolve.
21592175});
21602176` ` `
21612177
0 commit comments