Skip to content

Commit

Permalink
doc: never test for aborts using err.name
Browse files Browse the repository at this point in the history
Testing the rejected error is not really safe, especially if adopted
for third-party code. This also makes for a smoother transition if
node APIs adopt signal.reason.

Refs: nodejs#40692
Refs: whatwg/dom#1027
  • Loading branch information
kanongil committed Nov 19, 2021
1 parent e0233fc commit 7518743
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion doc/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ async function foo(emitter, event, signal) {
await once(emitter, event, { signal });
console.log('event emitted!');
} catch (error) {
if (error.name === 'AbortError') {
if (signal.aborted) {
console.error('Waiting for the event was canceled!');
} else {
console.error('There was an error', error.message);
Expand Down
2 changes: 1 addition & 1 deletion doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1484,7 +1484,7 @@ setTimeout(() => ac.abort(), 10000);
for await (const event of watcher)
console.log(event);
} catch (err) {
if (err.name === 'AbortError')
if (signal.aborted)
return;
throw err;
}
Expand Down
8 changes: 3 additions & 5 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -2357,18 +2357,16 @@ Or using an `AbortSignal` with a readable stream as an async iterable:

```js
const controller = new AbortController();
const { signal } = ac;
setTimeout(() => controller.abort(), 10_000); // set a timeout
const stream = addAbortSignal(
controller.signal,
fs.createReadStream(('object.json'))
);
const stream = addAbortSignal(signal, fs.createReadStream(('object.json')));
(async () => {
try {
for await (const chunk of stream) {
await process(chunk);
}
} catch (e) {
if (e.name === 'AbortError') {
if (signal.aborted) {
// The operation was cancelled
} else {
throw e;
Expand Down
4 changes: 2 additions & 2 deletions doc/api/timers.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ const signal = ac.signal;
setImmediatePromise('foobar', { signal })
.then(console.log)
.catch((err) => {
if (err.name === 'AbortError')
if (signal.aborted)
console.log('The immediate was aborted');
});

Expand All @@ -281,7 +281,7 @@ const signal = ac.signal;
setTimeoutPromise(1000, 'foobar', { signal })
.then(console.log)
.catch((err) => {
if (err.name === 'AbortError')
if (signal.aborted)
console.log('The timeout was aborted');
});

Expand Down

0 comments on commit 7518743

Please sign in to comment.