@@ -3019,6 +3019,10 @@ If `options.withFileTypes` is set to `true`, the result will contain
30193019<!-- YAML
30203020added: v0.1.29
30213021changes:
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.
30223026 - version: v10.0.0
30233027 pr-url: https://github.com/nodejs/node/pull/12562
30243028 description: The `callback` parameter is no longer optional. Not passing
@@ -3044,6 +3048,7 @@ changes:
30443048* ` options ` {Object|string}
30453049 * ` encoding ` {string|null} ** Default:** ` null `
30463050 * ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'r' ` .
3051+ * ` signal ` {AbortSignal} allows aborting an in-progress readFile
30473052* ` callback ` {Function}
30483053 * ` err ` {Error}
30493054 * ` data ` {string|Buffer}
@@ -3085,9 +3090,25 @@ fs.readFile('<directory>', (err, data) => {
30853090});
30863091```
30873092
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+
30883106The ` fs.readFile() ` function buffers the entire file. To minimize memory costs,
30893107when possible prefer streaming via ` fs.createReadStream() ` .
30903108
3109+ Aborting an ongoing request does not abort individual operating
3110+ system requests but rather the internal buffering ` fs.readFile ` performs.
3111+
30913112### File descriptors
30923113
309331141 . Any specified file descriptor has to support reading.
@@ -4734,6 +4755,7 @@ added: v10.0.0
47344755
47354756* ` options ` {Object|string}
47364757 * ` encoding ` {string|null} ** Default:** ` null `
4758+ * ` signal ` {AbortSignal} allows aborting an in-progress readFile
47374759* Returns: {Promise}
47384760
47394761Asynchronously reads the entire contents of a file.
@@ -5397,12 +5419,18 @@ print('./').catch(console.error);
53975419### ` fsPromises.readFile(path[, options]) `
53985420<!-- YAML
53995421added: 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.
54005427-->
54015428
54025429* ` path ` {string|Buffer|URL|FileHandle} filename or ` FileHandle `
54035430* ` options ` {Object|string}
54045431 * ` encoding ` {string|null} ** Default:** ` null `
54055432 * ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'r' ` .
5433+ * ` signal ` {AbortSignal} allows aborting an in-progress readFile
54065434* Returns: {Promise}
54075435
54085436Asynchronously reads the entire contents of a file.
@@ -5418,6 +5446,20 @@ platform-specific. On macOS, Linux, and Windows, the promise will be rejected
54185446with an error. On FreeBSD, a representation of the directory's contents will be
54195447returned.
54205448
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+
54215463Any specified ` FileHandle ` has to support reading.
54225464
54235465### ` fsPromises.readlink(path[, options]) `
0 commit comments