@@ -2248,39 +2248,55 @@ added:
22482248> Stability: 1 - Experimental
22492249
22502250* ` signal` {AbortSignal}
2251- * ` resource` {Object} Any non-null entity, reference to which is held weakly.
2251+ * ` resource` {Object} Any non-null object tied to the abortable operation and held weakly.
2252+ If ` resource` is garbage collected before the ` signal` aborts, the promise remains pending,
2253+ allowing Node.js to stop tracking it.
2254+ This helps prevent memory leaks in long-running or non-cancelable operations.
22522255* Returns: {Promise}
22532256
2254- Listens to abort event on the provided ` signal` and
2255- returns a promise that is fulfilled when the ` signal` is
2256- aborted. If the passed ` resource` is garbage collected before the ` signal` is
2257- aborted, the returned promise shall remain pending indefinitely.
2257+ Listens to abort event on the provided ` signal` and returns a promise that resolves when the ` signal` is aborted.
2258+ If ` resource` is provided, it weakly references the operation's associated object,
2259+ so if ` resource` is garbage collected before the ` signal` aborts,
2260+ then returned promise shall remain pending.
2261+ This prevents memory leaks in long-running or non-cancelable operations.
22582262
22592263` ` ` cjs
22602264const { aborted } = require (' node:util' );
22612265
2266+ // Obtain an object with an abortable signal, like a custom resource or operation.
22622267const dependent = obtainSomethingAbortable ();
22632268
2269+ // Pass `dependent` as the resource, indicating the promise should only resolve
2270+ // if `dependent` is still in memory when the signal is aborted.
22642271aborted (dependent .signal , dependent).then (() => {
2265- // Do something when dependent is aborted.
2272+
2273+ // This code runs when `dependent` is aborted.
2274+ console .log (' Dependent resource was aborted.' );
22662275});
22672276
2277+ // Simulate an event that triggers the abort.
22682278dependent .on (' event' , () => {
2269- dependent .abort ();
2279+ dependent .abort (); // This will cause the `aborted` promise to resolve.
22702280});
22712281` ` `
22722282
22732283` ` ` mjs
22742284import { aborted } from ' node:util' ;
22752285
2286+ // Obtain an object with an abortable signal, like a custom resource or operation.
22762287const dependent = obtainSomethingAbortable ();
22772288
2289+ // Pass `dependent` as the resource, indicating the promise should only resolve
2290+ // if `dependent` is still in memory when the signal is aborted.
22782291aborted (dependent .signal , dependent).then (() => {
2279- // Do something when dependent is aborted.
2292+
2293+ // This code runs when `dependent` is aborted.
2294+ console .log (' Dependent resource was aborted.' );
22802295});
22812296
2297+ // Simulate an event that triggers the abort.
22822298dependent .on (' event' , () => {
2283- dependent .abort ();
2299+ dependent .abort (); // This will cause the `aborted` promise to resolve.
22842300});
22852301` ` `
22862302
0 commit comments