@@ -3031,6 +3031,10 @@ If `options.withFileTypes` is set to `true`, the result will contain
3031
3031
<!-- YAML
3032
3032
added: v0.1.29
3033
3033
changes:
3034
+ - version: REPLACEME
3035
+ pr-url: https://github.com/nodejs/node/pull/35911
3036
+ description: The options argument may include an AbortSignal to abort an
3037
+ ongoing readFile request.
3034
3038
- version: v10.0.0
3035
3039
pr-url: https://github.com/nodejs/node/pull/12562
3036
3040
description: The `callback` parameter is no longer optional. Not passing
@@ -3056,6 +3060,7 @@ changes:
3056
3060
* ` options ` {Object|string}
3057
3061
* ` encoding ` {string|null} ** Default:** ` null `
3058
3062
* ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'r' ` .
3063
+ * ` signal ` {AbortSignal} allows aborting an in-progress readFile
3059
3064
* ` callback ` {Function}
3060
3065
* ` err ` {Error}
3061
3066
* ` data ` {string|Buffer}
@@ -3097,9 +3102,25 @@ fs.readFile('<directory>', (err, data) => {
3097
3102
});
3098
3103
```
3099
3104
3105
+ It is possible to abort an ongoing request using an ` AbortSignal ` . If a
3106
+ request is aborted the callback is called with an ` AbortError ` :
3107
+
3108
+ ``` js
3109
+ const controller = new AbortController ();
3110
+ const signal = controller .signal ;
3111
+ fs .readFile (fileInfo[0 ].name , { signal }, (err , buf ) => {
3112
+ // ...
3113
+ });
3114
+ // When you want to abort the request
3115
+ controller .abort ();
3116
+ ```
3117
+
3100
3118
The ` fs.readFile() ` function buffers the entire file. To minimize memory costs,
3101
3119
when possible prefer streaming via ` fs.createReadStream() ` .
3102
3120
3121
+ Aborting an ongoing request does not abort individual operating
3122
+ system requests but rather the internal buffering ` fs.readFile ` performs.
3123
+
3103
3124
### File descriptors
3104
3125
3105
3126
1 . Any specified file descriptor has to support reading.
@@ -4771,6 +4792,7 @@ added: v10.0.0
4771
4792
4772
4793
* ` options ` {Object|string}
4773
4794
* ` encoding ` {string|null} ** Default:** ` null `
4795
+ * ` signal ` {AbortSignal} allows aborting an in-progress readFile
4774
4796
* Returns: {Promise}
4775
4797
4776
4798
Asynchronously reads the entire contents of a file.
@@ -5438,12 +5460,18 @@ print('./').catch(console.error);
5438
5460
### ` fsPromises.readFile(path[, options]) `
5439
5461
<!-- YAML
5440
5462
added: v10.0.0
5463
+ changes:
5464
+ - version: REPLACEME
5465
+ pr-url: https://github.com/nodejs/node/pull/35911
5466
+ description: The options argument may include an AbortSignal to abort an
5467
+ ongoing readFile request.
5441
5468
-->
5442
5469
5443
5470
* ` path ` {string|Buffer|URL|FileHandle} filename or ` FileHandle `
5444
5471
* ` options ` {Object|string}
5445
5472
* ` encoding ` {string|null} ** Default:** ` null `
5446
5473
* ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'r' ` .
5474
+ * ` signal ` {AbortSignal} allows aborting an in-progress readFile
5447
5475
* Returns: {Promise}
5448
5476
5449
5477
Asynchronously reads the entire contents of a file.
@@ -5459,6 +5487,20 @@ platform-specific. On macOS, Linux, and Windows, the promise will be rejected
5459
5487
with an error. On FreeBSD, a representation of the directory's contents will be
5460
5488
returned.
5461
5489
5490
+ It is possible to abort an ongoing ` readFile ` using an ` AbortSignal ` . If a
5491
+ request is aborted the promise returned is rejected with an ` AbortError ` :
5492
+
5493
+ ``` js
5494
+ const controller = new AbortController ();
5495
+ const signal = controller .signal ;
5496
+ readFile (fileName, { signal }).then ((file ) => { /* ... */ });
5497
+ // Abort the request
5498
+ controller .abort ();
5499
+ ```
5500
+
5501
+ Aborting an ongoing request does not abort individual operating
5502
+ system requests but rather the internal buffering ` fs.readFile ` performs.
5503
+
5462
5504
Any specified ` FileHandle ` has to support reading.
5463
5505
5464
5506
### ` fsPromises.readlink(path[, options]) `
0 commit comments