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 @@ -984,12 +984,15 @@ import { readFile } from 'fs/promises';
984984
985985try {
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
10041007added: 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);
You can’t perform that action at this time.
0 commit comments