@@ -3019,6 +3019,10 @@ If `options.withFileTypes` is set to `true`, the result will contain
3019
3019
<!-- YAML
3020
3020
added: v0.1.29
3021
3021
changes:
3022
+ - version: REPLACEME
3023
+ pr-url: https://github.com/nodejs/node/pull/35911
3024
+ description: The options argument may include an AbortSignal to abort an
3025
+ ongoing readFile request.
3022
3026
- version: v10.0.0
3023
3027
pr-url: https://github.com/nodejs/node/pull/12562
3024
3028
description: The `callback` parameter is no longer optional. Not passing
@@ -3044,6 +3048,7 @@ changes:
3044
3048
* ` options ` {Object|string}
3045
3049
* ` encoding ` {string|null} ** Default:** ` null `
3046
3050
* ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'r' ` .
3051
+ * ` signal ` {AbortSignal} allows aborting an in-progress readFile
3047
3052
* ` callback ` {Function}
3048
3053
* ` err ` {Error}
3049
3054
* ` data ` {string|Buffer}
@@ -3085,9 +3090,25 @@ fs.readFile('<directory>', (err, data) => {
3085
3090
});
3086
3091
```
3087
3092
3093
+ It is possible to abort an ongoing request using an ` AbortSignal ` . If a
3094
+ request is aborted the callback is called with an ` AbortError ` :
3095
+
3096
+ ``` js
3097
+ const controller = new AbortController ();
3098
+ const signal = controller .signal ;
3099
+ fs .readFile (fileInfo[0 ].name , { signal }, (err , buf ) => {
3100
+ // ...
3101
+ });
3102
+ // When you want to abort the request
3103
+ controller .abort ();
3104
+ ```
3105
+
3088
3106
The ` fs.readFile() ` function buffers the entire file. To minimize memory costs,
3089
3107
when possible prefer streaming via ` fs.createReadStream() ` .
3090
3108
3109
+ Aborting an ongoing request does not abort individual operating
3110
+ system requests but rather the internal buffering ` fs.readFile ` performs.
3111
+
3091
3112
### File descriptors
3092
3113
3093
3114
1 . Any specified file descriptor has to support reading.
@@ -4734,6 +4755,7 @@ added: v10.0.0
4734
4755
4735
4756
* ` options ` {Object|string}
4736
4757
* ` encoding ` {string|null} ** Default:** ` null `
4758
+ * ` signal ` {AbortSignal} allows aborting an in-progress readFile
4737
4759
* Returns: {Promise}
4738
4760
4739
4761
Asynchronously reads the entire contents of a file.
@@ -5397,12 +5419,18 @@ print('./').catch(console.error);
5397
5419
### ` fsPromises.readFile(path[, options]) `
5398
5420
<!-- YAML
5399
5421
added: v10.0.0
5422
+ changes:
5423
+ - version: REPLACEME
5424
+ pr-url: https://github.com/nodejs/node/pull/35911
5425
+ description: The options argument may include an AbortSignal to abort an
5426
+ ongoing readFile request.
5400
5427
-->
5401
5428
5402
5429
* ` path ` {string|Buffer|URL|FileHandle} filename or ` FileHandle `
5403
5430
* ` options ` {Object|string}
5404
5431
* ` encoding ` {string|null} ** Default:** ` null `
5405
5432
* ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'r' ` .
5433
+ * ` signal ` {AbortSignal} allows aborting an in-progress readFile
5406
5434
* Returns: {Promise}
5407
5435
5408
5436
Asynchronously reads the entire contents of a file.
@@ -5418,6 +5446,20 @@ platform-specific. On macOS, Linux, and Windows, the promise will be rejected
5418
5446
with an error. On FreeBSD, a representation of the directory's contents will be
5419
5447
returned.
5420
5448
5449
+ It is possible to abort an ongoing ` readFile ` using an ` AbortSignal ` . If a
5450
+ request is aborted the promise returned is rejected with an ` AbortError ` :
5451
+
5452
+ ``` js
5453
+ const controller = new AbortController ();
5454
+ const signal = controller .signal ;
5455
+ readFile (fileName, { signal }).then ((file ) => { /* ... */ });
5456
+ // Abort the request
5457
+ controller .abort ();
5458
+ ```
5459
+
5460
+ Aborting an ongoing request does not abort individual operating
5461
+ system requests but rather the internal buffering ` fs.readFile ` performs.
5462
+
5421
5463
Any specified ` FileHandle ` has to support reading.
5422
5464
5423
5465
### ` fsPromises.readlink(path[, options]) `
0 commit comments