Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9502c22

Browse filesBrowse 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 6fec397 commit 9502c22
Copy full SHA for 9502c22

1 file changed

+12-18Lines changed: 12 additions & 18 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/events.md‎

Copy file name to clipboardExpand all lines: doc/api/events.md
+12-18Lines changed: 12 additions & 18 deletions
  • Display the source diff
  • Display the rich diff
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.