Skip to content

Commit 8590c15

Browse files
MoritzKntargos
authored andcommitted
doc: update abort signal in fs promise api example
PR-URL: #38669 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 5b25fbe commit 8590c15

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

doc/api/fs.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -980,12 +980,15 @@ import { readFile } from 'fs/promises';
980980
981981
try {
982982
const controller = new AbortController();
983-
const signal = controller.signal;
984-
readFile(fileName, { signal });
983+
const { signal } = controller;
984+
const promise = readFile(fileName, { signal });
985985
986-
// Abort the request
986+
// Abort the request before the promise settles.
987987
controller.abort();
988+
989+
await promise;
988990
} catch (err) {
991+
// When a request is aborted - err is an AbortError
989992
console.error(err);
990993
}
991994
```
@@ -999,7 +1002,6 @@ Any specified {FileHandle} has to support reading.
9991002
<!-- YAML
10001003
added: v10.0.0
10011004
-->
1002-
10031005
* `path` {string|Buffer|URL}
10041006
* `options` {string|Object}
10051007
* `encoding` {string} **Default:** `'utf8'`
@@ -1316,8 +1318,12 @@ try {
13161318
const controller = new AbortController();
13171319
const { signal } = controller;
13181320
const data = new Uint8Array(Buffer.from('Hello Node.js'));
1319-
writeFile('message.txt', data, { signal });
1321+
const promise = writeFile('message.txt', data, { signal });
1322+
1323+
// Abort the request before the promise settles.
13201324
controller.abort();
1325+
1326+
await promise;
13211327
} catch (err) {
13221328
// When a request is aborted - err is an AbortError
13231329
console.error(err);

0 commit comments

Comments
 (0)