@@ -3026,6 +3026,10 @@ If `options.withFileTypes` is set to `true`, the result will contain
30263026<!-- YAML
30273027added: v0.1.29
30283028changes:
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.
30293033 - version: v10.0.0
30303034 pr-url: https://github.com/nodejs/node/pull/12562
30313035 description: The `callback` parameter is no longer optional. Not passing
@@ -3051,6 +3055,7 @@ changes:
30513055* ` options ` {Object|string}
30523056 * ` encoding ` {string|null} ** Default:** ` null `
30533057 * ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'r' ` .
3058+ * ` signal ` {AbortSignal} allows aborting an in-progress readFile
30543059* ` callback ` {Function}
30553060 * ` err ` {Error}
30563061 * ` data ` {string|Buffer}
@@ -3092,9 +3097,25 @@ fs.readFile('<directory>', (err, data) => {
30923097});
30933098```
30943099
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+
30953113The ` fs.readFile() ` function buffers the entire file. To minimize memory costs,
30963114when possible prefer streaming via ` fs.createReadStream() ` .
30973115
3116+ Aborting an ongoing request does not abort individual operating
3117+ system requests but rather the internal buffering ` fs.readFile ` performs.
3118+
30983119### File descriptors
30993120
310031211 . Any specified file descriptor has to support reading.
@@ -4748,6 +4769,7 @@ added: v10.0.0
47484769
47494770* ` options ` {Object|string}
47504771 * ` encoding ` {string|null} ** Default:** ` null `
4772+ * ` signal ` {AbortSignal} allows aborting an in-progress readFile
47514773* Returns: {Promise}
47524774
47534775Asynchronously reads the entire contents of a file.
@@ -5411,12 +5433,18 @@ print('./').catch(console.error);
54115433### ` fsPromises.readFile(path[, options]) `
54125434<!-- YAML
54135435added: 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.
54145441-->
54155442
54165443* ` path ` {string|Buffer|URL|FileHandle} filename or ` FileHandle `
54175444* ` options ` {Object|string}
54185445 * ` encoding ` {string|null} ** Default:** ` null `
54195446 * ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'r' ` .
5447+ * ` signal ` {AbortSignal} allows aborting an in-progress readFile
54205448* Returns: {Promise}
54215449
54225450Asynchronously reads the entire contents of a file.
@@ -5432,6 +5460,20 @@ platform-specific. On macOS, Linux, and Windows, the promise will be rejected
54325460with an error. On FreeBSD, a representation of the directory's contents will be
54335461returned.
54345462
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+
54355477Any specified ` FileHandle ` has to support reading.
54365478
54375479### ` fsPromises.readlink(path[, options]) `
0 commit comments