Skip to content

Commit 548e1d7

Browse files
atlowChemiaduh95
authored andcommitted
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 58d1f2e commit 548e1d7

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
@@ -1906,31 +1906,25 @@ Returns a disposable so that it may be unsubscribed from more easily.
19061906
const { addAbortListener } = require('node:events');
19071907

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

19211918
```mjs
19221919
import { addAbortListener } from 'node:events';
19231920

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

0 commit comments

Comments
 (0)