@@ -3026,6 +3026,10 @@ If `options.withFileTypes` is set to `true`, the result will contain
3026
3026
<!-- YAML
3027
3027
added: v0.1.29
3028
3028
changes:
3029
+ - version: REPLACEME
3030
+ pr-url: https://github.com/nodejs/node/pull/35911
3031
+ description: The options argument may include an AbortSignal to abort an
3032
+ ongoing readFile request.
3029
3033
- version: v10.0.0
3030
3034
pr-url: https://github.com/nodejs/node/pull/12562
3031
3035
description: The `callback` parameter is no longer optional. Not passing
@@ -3051,6 +3055,7 @@ changes:
3051
3055
* ` options ` {Object|string}
3052
3056
* ` encoding ` {string|null} ** Default:** ` null `
3053
3057
* ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'r' ` .
3058
+ * ` signal ` {AbortSignal} allows aborting an in-progress readFile
3054
3059
* ` callback ` {Function}
3055
3060
* ` err ` {Error}
3056
3061
* ` data ` {string|Buffer}
@@ -3092,9 +3097,25 @@ fs.readFile('<directory>', (err, data) => {
3092
3097
});
3093
3098
```
3094
3099
3100
+ It is possible to abort an ongoing request using an ` AbortSignal ` . If a
3101
+ request is aborted the callback is called with an ` AbortError ` :
3102
+
3103
+ ``` js
3104
+ const controller = new AbortController ();
3105
+ const signal = controller .signal ;
3106
+ fs .readFile (fileInfo[0 ].name , { signal }, (err , buf ) => {
3107
+ // ...
3108
+ });
3109
+ // When you want to abort the request
3110
+ controller .abort ();
3111
+ ```
3112
+
3095
3113
The ` fs.readFile() ` function buffers the entire file. To minimize memory costs,
3096
3114
when possible prefer streaming via ` fs.createReadStream() ` .
3097
3115
3116
+ Aborting an ongoing request does not abort individual operating
3117
+ system requests but rather the internal buffering ` fs.readFile ` performs.
3118
+
3098
3119
### File descriptors
3099
3120
3100
3121
1 . Any specified file descriptor has to support reading.
@@ -4748,6 +4769,7 @@ added: v10.0.0
4748
4769
4749
4770
* ` options ` {Object|string}
4750
4771
* ` encoding ` {string|null} ** Default:** ` null `
4772
+ * ` signal ` {AbortSignal} allows aborting an in-progress readFile
4751
4773
* Returns: {Promise}
4752
4774
4753
4775
Asynchronously reads the entire contents of a file.
@@ -5411,12 +5433,18 @@ print('./').catch(console.error);
5411
5433
### ` fsPromises.readFile(path[, options]) `
5412
5434
<!-- YAML
5413
5435
added: v10.0.0
5436
+ changes:
5437
+ - version: REPLACEME
5438
+ pr-url: https://github.com/nodejs/node/pull/35911
5439
+ description: The options argument may include an AbortSignal to abort an
5440
+ ongoing readFile request.
5414
5441
-->
5415
5442
5416
5443
* ` path ` {string|Buffer|URL|FileHandle} filename or ` FileHandle `
5417
5444
* ` options ` {Object|string}
5418
5445
* ` encoding ` {string|null} ** Default:** ` null `
5419
5446
* ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'r' ` .
5447
+ * ` signal ` {AbortSignal} allows aborting an in-progress readFile
5420
5448
* Returns: {Promise}
5421
5449
5422
5450
Asynchronously reads the entire contents of a file.
@@ -5432,6 +5460,20 @@ platform-specific. On macOS, Linux, and Windows, the promise will be rejected
5432
5460
with an error. On FreeBSD, a representation of the directory's contents will be
5433
5461
returned.
5434
5462
5463
+ It is possible to abort an ongoing ` readFile ` using an ` AbortSignal ` . If a
5464
+ request is aborted the promise returned is rejected with an ` AbortError ` :
5465
+
5466
+ ``` js
5467
+ const controller = new AbortController ();
5468
+ const signal = controller .signal ;
5469
+ readFile (fileName, { signal }).then ((file ) => { /* ... */ });
5470
+ // Abort the request
5471
+ controller .abort ();
5472
+ ```
5473
+
5474
+ Aborting an ongoing request does not abort individual operating
5475
+ system requests but rather the internal buffering ` fs.readFile ` performs.
5476
+
5435
5477
Any specified ` FileHandle ` has to support reading.
5436
5478
5437
5479
### ` fsPromises.readlink(path[, options]) `
0 commit comments