File tree Expand file tree Collapse file tree 1 file changed +11
-5
lines changed Expand file tree Collapse file tree 1 file changed +11
-5
lines changed Original file line number Diff line number Diff line change @@ -980,12 +980,15 @@ import { readFile } from 'fs/promises';
980980
981981try {
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
10001003added: 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);
You can’t perform that action at this time.
0 commit comments