Skip to content

Commit ca90ab1

Browse files
authored
doc: simplify addAbortListener example
The example was written before v8 supported the using keyword and hence explicitly called Symbol.dispose Since now the keyword is supported, the example can be simplified PR-URL: #61842 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent b4d8774 commit ca90ab1

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

doc/api/events.md

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,31 +1908,25 @@ Returns a disposable so that it may be unsubscribed from more easily.
19081908
const { addAbortListener } = require('node:events');
19091909

19101910
function example(signal) {
1911-
let disposable;
1912-
try {
1913-
signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
1914-
disposable = addAbortListener(signal, (e) => {
1915-
// Do something when signal is aborted.
1916-
});
1917-
} finally {
1918-
disposable?.[Symbol.dispose]();
1919-
}
1911+
signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
1912+
// addAbortListener() returns a disposable, so the `using` keyword ensures
1913+
// the abort listener is automatically removed when this scope exits.
1914+
using _ = addAbortListener(signal, (e) => {
1915+
// Do something when signal is aborted.
1916+
});
19201917
}
19211918
```
19221919

19231920
```mjs
19241921
import { addAbortListener } from 'node:events';
19251922

19261923
function example(signal) {
1927-
let disposable;
1928-
try {
1929-
signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
1930-
disposable = addAbortListener(signal, (e) => {
1931-
// Do something when signal is aborted.
1932-
});
1933-
} finally {
1934-
disposable?.[Symbol.dispose]();
1935-
}
1924+
signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
1925+
// addAbortListener() returns a disposable, so the `using` keyword ensures
1926+
// the abort listener is automatically removed when this scope exits.
1927+
using _ = addAbortListener(signal, (e) => {
1928+
// Do something when signal is aborted.
1929+
});
19361930
}
19371931
```
19381932

0 commit comments

Comments
 (0)