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 b50376f

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 dea0e7a commit b50376f
Copy full SHA for b50376f

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
@@ -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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.