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