doc: clarify util.aborted resource usage · nodejs/node@0d08756
@@ -2244,39 +2244,55 @@ added:
22442244> Stability: 1 - Experimental
2245224522462246* `signal` {AbortSignal}
2247-* `resource` {Object} Any non-null entity, reference to which is held weakly.
2247+* `resource` {Object} Any non-null object tied to the abortable operation and held weakly.
2248+ If `resource` is garbage collected before the `signal` aborts, the promise remains pending,
2249+ allowing Node.js to stop tracking it.
2250+ This helps prevent memory leaks in long-running or non-cancelable operations.
22482251* Returns: {Promise}
224922522250-Listens to abort event on the provided `signal` and
2251-returns a promise that is fulfilled when the `signal` is
2252-aborted. If the passed `resource` is garbage collected before the `signal` is
2253-aborted, the returned promise shall remain pending indefinitely.
2253+Listens to abort event on the provided `signal` and returns a promise that resolves when the `signal` is aborted.
2254+If `resource` is provided, it weakly references the operation's associated object,
2255+so if `resource` is garbage collected before the `signal` aborts,
2256+then returned promise shall remain pending.
2257+This prevents memory leaks in long-running or non-cancelable operations.
2254225822552259```cjs
22562260const { aborted } = require('node:util');
225722612262+// Obtain an object with an abortable signal, like a custom resource or operation.
22582263const dependent = obtainSomethingAbortable();
225922642265+// Pass `dependent` as the resource, indicating the promise should only resolve
2266+// if `dependent` is still in memory when the signal is aborted.
22602267aborted(dependent.signal, dependent).then(() => {
2261-// Do something when dependent is aborted.
2268+2269+// This code runs when `dependent` is aborted.
2270+console.log('Dependent resource was aborted.');
22622271});
226322722273+// Simulate an event that triggers the abort.
22642274dependent.on('event', () => {
2265-dependent.abort();
2275+dependent.abort(); // This will cause the `aborted` promise to resolve.
22662276});
22672277```
2268227822692279```mjs
22702280import { aborted } from 'node:util';
227122812282+// Obtain an object with an abortable signal, like a custom resource or operation.
22722283const dependent = obtainSomethingAbortable();
227322842285+// Pass `dependent` as the resource, indicating the promise should only resolve
2286+// if `dependent` is still in memory when the signal is aborted.
22742287aborted(dependent.signal, dependent).then(() => {
2275-// Do something when dependent is aborted.
2288+2289+// This code runs when `dependent` is aborted.
2290+console.log('Dependent resource was aborted.');
22762291});
227722922293+// Simulate an event that triggers the abort.
22782294dependent.on('event', () => {
2279-dependent.abort();
2295+dependent.abort(); // This will cause the `aborted` promise to resolve.
22802296});
22812297```
22822298