Skip to content

Commit e7f3f0d

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 a44219d commit e7f3f0d

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
@@ -984,12 +984,15 @@ import { readFile } from 'fs/promises';
984984
985985
try {
986986
const controller = new AbortController();
987-
const signal = controller.signal;
988-
readFile(fileName, { signal });
987+
const { signal } = controller;
988+
const promise = readFile(fileName, { signal });
989989
990-
// Abort the request
990+
// Abort the request before the promise settles.
991991
controller.abort();
992+
993+
await promise;
992994
} catch (err) {
995+
// When a request is aborted - err is an AbortError
993996
console.error(err);
994997
}
995998
```
@@ -1003,7 +1006,6 @@ Any specified {FileHandle} has to support reading.
10031006
<!-- YAML
10041007
added: v10.0.0
10051008
-->
1006-
10071009
* `path` {string|Buffer|URL}
10081010
* `options` {string|Object}
10091011
* `encoding` {string} **Default:** `'utf8'`
@@ -1309,8 +1311,12 @@ try {
13091311
const controller = new AbortController();
13101312
const { signal } = controller;
13111313
const data = new Uint8Array(Buffer.from('Hello Node.js'));
1312-
writeFile('message.txt', data, { signal });
1314+
const promise = writeFile('message.txt', data, { signal });
1315+
1316+
// Abort the request before the promise settles.
13131317
controller.abort();
1318+
1319+
await promise;
13141320
} catch (err) {
13151321
// When a request is aborted - err is an AbortError
13161322
console.error(err);

0 commit comments

Comments
 (0)