@@ -4385,6 +4385,10 @@ details.
43854385<!-- YAML
43864386added: v0.1.29
43874387changes:
4388+ - version: REPLACEME
4389+ pr-url: https://github.com/nodejs/node/pull/35993
4390+ description: The options argument may include an AbortSignal to abort an
4391+ ongoing writeFile request.
43884392 - version: v14.12.0
43894393 pr-url: https://github.com/nodejs/node/pull/34993
43904394 description: The `data` parameter will stringify an object with an
@@ -4419,6 +4423,7 @@ changes:
44194423 * ` encoding ` {string|null} ** Default:** ` 'utf8' `
44204424 * ` mode ` {integer} ** Default:** ` 0o666 `
44214425 * ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'w' ` .
4426+ * ` signal ` {AbortSignal} allows aborting an in-progress writeFile
44224427* ` callback ` {Function}
44234428 * ` err ` {Error}
44244429
@@ -4450,6 +4455,28 @@ It is unsafe to use `fs.writeFile()` multiple times on the same file without
44504455waiting for the callback. For this scenario, [ ` fs.createWriteStream() ` ] [ ] is
44514456recommended.
44524457
4458+ Similarly to ` fs.readFile ` - ` fs.writeFile ` is a convenience method that
4459+ performs multiple ` write ` calls internally to write the buffer passed to it.
4460+ For performance sensitive code consider using [ ` fs.createWriteStream() ` ] [ ] .
4461+
4462+ It is possible to use an {AbortSignal} to cancel an ` fs.writeFile() ` .
4463+ Cancelation is "best effort", and some amount of data is likely still
4464+ to be written.
4465+
4466+ ``` js
4467+ const controller = new AbortController ();
4468+ const { signal } = controller;
4469+ const data = new Uint8Array (Buffer .from (' Hello Node.js' ));
4470+ fs .writeFile (' message.txt' , data, { signal }, (err ) => {
4471+ // When a request is aborted - the callback is called with an AbortError
4472+ });
4473+ // When the request should be aborted
4474+ controller .abort ();
4475+ ```
4476+
4477+ Aborting an ongoing request does not abort individual operating
4478+ system requests but rather the internal buffering ` fs.writeFile ` performs.
4479+
44534480### Using ` fs.writeFile() ` with file descriptors
44544481
44554482When ` file ` is a file descriptor, the behavior is almost identical to directly
@@ -5717,6 +5744,10 @@ The `atime` and `mtime` arguments follow these rules:
57175744<!-- YAML
57185745added: v10.0.0
57195746changes:
5747+ - version: REPLACEME
5748+ pr-url: https://github.com/nodejs/node/pull/35993
5749+ description: The options argument may include an AbortSignal to abort an
5750+ ongoing writeFile request.
57205751 - version: v14.12.0
57215752 pr-url: https://github.com/nodejs/node/pull/34993
57225753 description: The `data` parameter will stringify an object with an
@@ -5733,6 +5764,7 @@ changes:
57335764 * ` encoding ` {string|null} ** Default:** ` 'utf8' `
57345765 * ` mode ` {integer} ** Default:** ` 0o666 `
57355766 * ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'w' ` .
5767+ * ` signal ` {AbortSignal} allows aborting an in-progress writeFile
57365768* Returns: {Promise}
57375769
57385770Asynchronously writes data to a file, replacing the file if it already exists.
@@ -5746,7 +5778,34 @@ If `options` is a string, then it specifies the encoding.
57465778Any specified ` FileHandle ` has to support writing.
57475779
57485780It is unsafe to use ` fsPromises.writeFile() ` multiple times on the same file
5749- without waiting for the ` Promise ` to be resolved (or rejected).
5781+ without waiting for the ` Promise ` to be fulfilled (or rejected).
5782+
5783+ Similarly to ` fsPromises.readFile ` - ` fsPromises.writeFile ` is a convenience
5784+ method that performs multiple ` write ` calls internally to write the buffer
5785+ passed to it. For performance sensitive code consider using
5786+ [ ` fs.createWriteStream() ` ] [ ] .
5787+
5788+ It is possible to use an {AbortSignal} to cancel an ` fsPromises.writeFile() ` .
5789+ Cancelation is "best effort", and some amount of data is likely still
5790+ to be written.
5791+
5792+ ``` js
5793+ const controller = new AbortController ();
5794+ const { signal } = controller;
5795+ const data = new Uint8Array (Buffer .from (' Hello Node.js' ));
5796+ (async () => {
5797+ try {
5798+ await fs .writeFile (' message.txt' , data, { signal });
5799+ } catch (err) {
5800+ // When a request is aborted - err is an AbortError
5801+ }
5802+ })();
5803+ // When the request should be aborted
5804+ controller .abort ();
5805+ ```
5806+
5807+ Aborting an ongoing request does not abort individual operating
5808+ system requests but rather the internal buffering ` fs.writeFile ` performs.
57505809
57515810## FS constants
57525811
0 commit comments