Skip to content

Commit 0d08756

Browse files
KunalKumar-1aduh95
authored andcommitted
doc: clarify util.aborted resource usage
PR-URL: #55780 Fixes: #55340 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jason Zhang <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent f264dd6 commit 0d08756

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

doc/api/util.md

+25-9
Original file line numberDiff line numberDiff line change
@@ -2244,39 +2244,55 @@ added:
22442244
> Stability: 1 - Experimental
22452245
22462246
* `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}
22492252
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.
22542258
22552259
```cjs
22562260
const { aborted } = require('node:util');
22572261

2262+
// Obtain an object with an abortable signal, like a custom resource or operation.
22582263
const dependent = obtainSomethingAbortable();
22592264

2265+
// Pass `dependent` as the resource, indicating the promise should only resolve
2266+
// if `dependent` is still in memory when the signal is aborted.
22602267
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.');
22622271
});
22632272

2273+
// Simulate an event that triggers the abort.
22642274
dependent.on('event', () => {
2265-
dependent.abort();
2275+
dependent.abort(); // This will cause the `aborted` promise to resolve.
22662276
});
22672277
```
22682278
22692279
```mjs
22702280
import { aborted } from 'node:util';
22712281

2282+
// Obtain an object with an abortable signal, like a custom resource or operation.
22722283
const dependent = obtainSomethingAbortable();
22732284

2285+
// Pass `dependent` as the resource, indicating the promise should only resolve
2286+
// if `dependent` is still in memory when the signal is aborted.
22742287
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.');
22762291
});
22772292

2293+
// Simulate an event that triggers the abort.
22782294
dependent.on('event', () => {
2279-
dependent.abort();
2295+
dependent.abort(); // This will cause the `aborted` promise to resolve.
22802296
});
22812297
```
22822298

0 commit comments

Comments
 (0)