Skip to content

Commit a51ef9d

Browse files
KunalKumar-1marco-ippolito
authored andcommitted
doc: clarify util.aborted resource usage
PR-URL: #55780 Fixes: #55340 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jason Zhang <xzha4350@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
1 parent ea1c97a commit a51ef9d

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
@@ -2123,39 +2123,55 @@ added: v19.7.0
21232123
> Stability: 1 - Experimental
21242124
21252125
* `signal` {AbortSignal}
2126-
* `resource` {Object} Any non-null entity, reference to which is held weakly.
2126+
* `resource` {Object} Any non-null object tied to the abortable operation and held weakly.
2127+
If `resource` is garbage collected before the `signal` aborts, the promise remains pending,
2128+
allowing Node.js to stop tracking it.
2129+
This helps prevent memory leaks in long-running or non-cancelable operations.
21272130
* Returns: {Promise}
21282131
2129-
Listens to abort event on the provided `signal` and
2130-
returns a promise that is fulfilled when the `signal` is
2131-
aborted. If the passed `resource` is garbage collected before the `signal` is
2132-
aborted, the returned promise shall remain pending indefinitely.
2132+
Listens to abort event on the provided `signal` and returns a promise that resolves when the `signal` is aborted.
2133+
If `resource` is provided, it weakly references the operation's associated object,
2134+
so if `resource` is garbage collected before the `signal` aborts,
2135+
then returned promise shall remain pending.
2136+
This prevents memory leaks in long-running or non-cancelable operations.
21332137
21342138
```cjs
21352139
const { aborted } = require('node:util');
21362140

2141+
// Obtain an object with an abortable signal, like a custom resource or operation.
21372142
const dependent = obtainSomethingAbortable();
21382143

2144+
// Pass `dependent` as the resource, indicating the promise should only resolve
2145+
// if `dependent` is still in memory when the signal is aborted.
21392146
aborted(dependent.signal, dependent).then(() => {
2140-
// Do something when dependent is aborted.
2147+
2148+
// This code runs when `dependent` is aborted.
2149+
console.log('Dependent resource was aborted.');
21412150
});
21422151

2152+
// Simulate an event that triggers the abort.
21432153
dependent.on('event', () => {
2144-
dependent.abort();
2154+
dependent.abort(); // This will cause the `aborted` promise to resolve.
21452155
});
21462156
```
21472157
21482158
```mjs
21492159
import { aborted } from 'node:util';
21502160

2161+
// Obtain an object with an abortable signal, like a custom resource or operation.
21512162
const dependent = obtainSomethingAbortable();
21522163

2164+
// Pass `dependent` as the resource, indicating the promise should only resolve
2165+
// if `dependent` is still in memory when the signal is aborted.
21532166
aborted(dependent.signal, dependent).then(() => {
2154-
// Do something when dependent is aborted.
2167+
2168+
// This code runs when `dependent` is aborted.
2169+
console.log('Dependent resource was aborted.');
21552170
});
21562171

2172+
// Simulate an event that triggers the abort.
21572173
dependent.on('event', () => {
2158-
dependent.abort();
2174+
dependent.abort(); // This will cause the `aborted` promise to resolve.
21592175
});
21602176
```
21612177

0 commit comments

Comments
 (0)