You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/api/util.md
+18-9Lines changed: 18 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -2240,39 +2240,48 @@ added:
2240
2240
> Stability: 1 - Experimental
2241
2241
2242
2242
* `signal` {AbortSignal}
2243
-
* `resource` {Object} Any non-null entity, reference to which is held weakly.
2243
+
* `resource` {Object} Any non-null object tied to the abortable operation and held weakly. If `resource` is garbage collected before the `signal` aborts, the promise remains pending, allowing Node.js to stop tracking it. This helps prevent memory leaks in long-running or non-cancelable operations.
2244
2244
* Returns: {Promise}
2245
2245
2246
-
Listens to abort event on the provided `signal` and
2247
-
returns a promise that is fulfilled when the `signal` is
2248
-
aborted. If the passed `resource` is garbage collected before the `signal` is
2249
-
aborted, the returned promise shall remain pending indefinitely.
2246
+
Listens to abort event on the provided `signal` and returns a promise that resolves when the `signal` is aborted. If `resource` is provided, it weakly references the operation's associated object, so if `resource` is garbage collected before the `signal` aborts, then returned promise shall remain pending. This prevents memory leaks in long-running or non-cancelable operations.
2250
2247
2251
2248
```cjs
2252
2249
const { aborted } =require('node:util');
2253
2250
2251
+
// Obtain an object with an abortable signal, like a custom resource or operation.
2254
2252
constdependent=obtainSomethingAbortable();
2255
2253
2254
+
// Pass `dependent` as the resource, indicating the promise should only resolve
2255
+
// if `dependent` is still in memory when the signal is aborted.
2256
2256
aborted(dependent.signal, dependent).then(() => {
2257
-
// Do something when dependent is aborted.
2257
+
2258
+
// This code runs when `dependent` is aborted.
2259
+
console.log('Dependent resource was aborted.');
2258
2260
});
2259
2261
2262
+
// Simulate an event that triggers the abort.
2260
2263
dependent.on('event', () => {
2261
-
dependent.abort();
2264
+
dependent.abort();// This will cause the `aborted` promise to resolve.
2262
2265
});
2263
2266
```
2264
2267
2265
2268
```mjs
2266
2269
import { aborted } from'node:util';
2267
2270
2271
+
// Obtain an object with an abortable signal, like a custom resource or operation.
2268
2272
constdependent=obtainSomethingAbortable();
2269
2273
2274
+
// Pass `dependent` as the resource, indicating the promise should only resolve
2275
+
// if `dependent` is still in memory when the signal is aborted.
2270
2276
aborted(dependent.signal, dependent).then(() => {
2271
-
// Do something when dependent is aborted.
2277
+
2278
+
// This code runs when `dependent` is aborted.
2279
+
console.log('Dependent resource was aborted.');
2272
2280
});
2273
2281
2282
+
// Simulate an event that triggers the abort.
2274
2283
dependent.on('event', () => {
2275
-
dependent.abort();
2284
+
dependent.abort();// This will cause the `aborted` promise to resolve.
0 commit comments