Skip to content

Commit

Permalink
doc: update abort signal in fs promise api example
Browse files Browse the repository at this point in the history
PR-URL: #38669
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
MoritzKn authored and targos committed Sep 4, 2021
1 parent a44219d commit e7f3f0d
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -984,12 +984,15 @@ import { readFile } from 'fs/promises';
try {
const controller = new AbortController();
const signal = controller.signal;
readFile(fileName, { signal });
const { signal } = controller;
const promise = readFile(fileName, { signal });
// Abort the request
// Abort the request before the promise settles.
controller.abort();
await promise;
} catch (err) {
// When a request is aborted - err is an AbortError
console.error(err);
}
```
Expand All @@ -1003,7 +1006,6 @@ Any specified {FileHandle} has to support reading.
<!-- YAML
added: v10.0.0
-->
* `path` {string|Buffer|URL}
* `options` {string|Object}
* `encoding` {string} **Default:** `'utf8'`
Expand Down Expand Up @@ -1309,8 +1311,12 @@ try {
const controller = new AbortController();
const { signal } = controller;
const data = new Uint8Array(Buffer.from('Hello Node.js'));
writeFile('message.txt', data, { signal });
const promise = writeFile('message.txt', data, { signal });
// Abort the request before the promise settles.
controller.abort();
await promise;
} catch (err) {
// When a request is aborted - err is an AbortError
console.error(err);
Expand Down

0 comments on commit e7f3f0d

Please sign in to comment.