diff --git a/doc/api/events.md b/doc/api/events.md index 1accb787e18fcf..08b34383d7d183 100644 --- a/doc/api/events.md +++ b/doc/api/events.md @@ -1908,15 +1908,12 @@ 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()); + // addAbortListener() returns a disposable, so the `using` keyword ensures + // the abort listener is automatically removed when this scope exits. + using _ = addAbortListener(signal, (e) => { + // Do something when signal is aborted. + }); } ``` @@ -1924,15 +1921,12 @@ function example(signal) { 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()); + // addAbortListener() returns a disposable, so the `using` keyword ensures + // the abort listener is automatically removed when this scope exits. + using _ = addAbortListener(signal, (e) => { + // Do something when signal is aborted. + }); } ```