@@ -2244,39 +2244,55 @@ added:
2244
2244
> Stability: 1 - Experimental
2245
2245
2246
2246
* ` 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.
2248
2251
* Returns: {Promise}
2249
2252
2250
- 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.
2254
2258
2255
2259
` ` ` cjs
2256
2260
const { aborted } = require (' node:util' );
2257
2261
2262
+ // Obtain an object with an abortable signal, like a custom resource or operation.
2258
2263
const dependent = obtainSomethingAbortable ();
2259
2264
2265
+ // Pass `dependent` as the resource, indicating the promise should only resolve
2266
+ // if `dependent` is still in memory when the signal is aborted.
2260
2267
aborted (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.' );
2262
2271
});
2263
2272
2273
+ // Simulate an event that triggers the abort.
2264
2274
dependent .on (' event' , () => {
2265
- dependent .abort ();
2275
+ dependent .abort (); // This will cause the `aborted` promise to resolve.
2266
2276
});
2267
2277
` ` `
2268
2278
2269
2279
` ` ` mjs
2270
2280
import { aborted } from ' node:util' ;
2271
2281
2282
+ // Obtain an object with an abortable signal, like a custom resource or operation.
2272
2283
const dependent = obtainSomethingAbortable ();
2273
2284
2285
+ // Pass `dependent` as the resource, indicating the promise should only resolve
2286
+ // if `dependent` is still in memory when the signal is aborted.
2274
2287
aborted (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.' );
2276
2291
});
2277
2292
2293
+ // Simulate an event that triggers the abort.
2278
2294
dependent .on (' event' , () => {
2279
- dependent .abort ();
2295
+ dependent .abort (); // This will cause the `aborted` promise to resolve.
2280
2296
});
2281
2297
` ` `
2282
2298
0 commit comments