Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions doc/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -1908,31 +1908,21 @@ Returns a disposable so that it may be unsubscribed from more easily.
const { addAbortListener } = require('node:events');

function example(signal) {
let disposable;
try {
signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
disposable = addAbortListener(signal, (e) => {
// Do something when signal is aborted.
});
} finally {
disposable?.[Symbol.dispose]();
}
signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
using _ = addAbortListener(signal, (e) => {
// Do something when signal is aborted.
});
Comment on lines +1912 to +1914
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be worth adding a comment underneath to state that the abort listener is automatically removed when leaving the scope, otherwise the significance of the using statement doesn't have any immediate context to the casual observer.

}
```

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

function example(signal) {
let disposable;
try {
signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
disposable = addAbortListener(signal, (e) => {
// Do something when signal is aborted.
});
} finally {
disposable?.[Symbol.dispose]();
}
signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
using _ = addAbortListener(signal, (e) => {
// Do something when signal is aborted.
});
}
```

Expand Down
Loading