@@ -2123,39 +2123,55 @@ added: v19.7.0
2123
2123
> Stability: 1 - Experimental
2124
2124
2125
2125
* ` 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.
2127
2130
* Returns: {Promise}
2128
2131
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.
2133
2137
2134
2138
` ` ` cjs
2135
2139
const { aborted } = require (' node:util' );
2136
2140
2141
+ // Obtain an object with an abortable signal, like a custom resource or operation.
2137
2142
const dependent = obtainSomethingAbortable ();
2138
2143
2144
+ // Pass `dependent` as the resource, indicating the promise should only resolve
2145
+ // if `dependent` is still in memory when the signal is aborted.
2139
2146
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.' );
2141
2150
});
2142
2151
2152
+ // Simulate an event that triggers the abort.
2143
2153
dependent .on (' event' , () => {
2144
- dependent .abort ();
2154
+ dependent .abort (); // This will cause the `aborted` promise to resolve.
2145
2155
});
2146
2156
` ` `
2147
2157
2148
2158
` ` ` mjs
2149
2159
import { aborted } from ' node:util' ;
2150
2160
2161
+ // Obtain an object with an abortable signal, like a custom resource or operation.
2151
2162
const dependent = obtainSomethingAbortable ();
2152
2163
2164
+ // Pass `dependent` as the resource, indicating the promise should only resolve
2165
+ // if `dependent` is still in memory when the signal is aborted.
2153
2166
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.' );
2155
2170
});
2156
2171
2172
+ // Simulate an event that triggers the abort.
2157
2173
dependent .on (' event' , () => {
2158
- dependent .abort ();
2174
+ dependent .abort (); // This will cause the `aborted` promise to resolve.
2159
2175
});
2160
2176
` ` `
2161
2177
0 commit comments