@@ -4375,6 +4375,10 @@ details.
43754375<!-- YAML
43764376added: v0.1.29
43774377changes:
4378+ - version: REPLACEME
4379+ pr-url: https://github.com/nodejs/node/pull/35993
4380+ description: The options argument may include an AbortSignal to abort an
4381+ ongoing writeFile request.
43784382 - version: v14.12.0
43794383 pr-url: https://github.com/nodejs/node/pull/34993
43804384 description: The `data` parameter will stringify an object with an
@@ -4409,6 +4413,7 @@ changes:
44094413 * ` encoding ` {string|null} ** Default:** ` 'utf8' `
44104414 * ` mode ` {integer} ** Default:** ` 0o666 `
44114415 * ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'w' ` .
4416+ * ` signal ` {AbortSignal} allows aborting an in-progress writeFile
44124417* ` callback ` {Function}
44134418 * ` err ` {Error}
44144419
@@ -4440,6 +4445,28 @@ It is unsafe to use `fs.writeFile()` multiple times on the same file without
44404445waiting for the callback. For this scenario, [ ` fs.createWriteStream() ` ] [ ] is
44414446recommended.
44424447
4448+ Similarly to ` fs.readFile ` - ` fs.writeFile ` is a convenience method that
4449+ performs multiple ` write ` calls internally to write the buffer passed to it.
4450+ For performance sensitive code consider using [ ` fs.createWriteStream() ` ] [ ] .
4451+
4452+ It is possible to use an {AbortSignal} to cancel an ` fs.writeFile() ` .
4453+ Cancelation is "best effort", and some amount of data is likely still
4454+ to be written.
4455+
4456+ ``` js
4457+ const controller = new AbortController ();
4458+ const { signal } = controller;
4459+ const data = new Uint8Array (Buffer .from (' Hello Node.js' ));
4460+ fs .writeFile (' message.txt' , data, { signal }, (err ) => {
4461+ // When a request is aborted - the callback is called with an AbortError
4462+ });
4463+ // When the request should be aborted
4464+ controller .abort ();
4465+ ```
4466+
4467+ Aborting an ongoing request does not abort individual operating
4468+ system requests but rather the internal buffering ` fs.writeFile ` performs.
4469+
44434470### Using ` fs.writeFile() ` with file descriptors
44444471
44454472When ` file ` is a file descriptor, the behavior is almost identical to directly
@@ -5684,6 +5711,10 @@ The `atime` and `mtime` arguments follow these rules:
56845711<!-- YAML
56855712added: v10.0.0
56865713changes:
5714+ - version: REPLACEME
5715+ pr-url: https://github.com/nodejs/node/pull/35993
5716+ description: The options argument may include an AbortSignal to abort an
5717+ ongoing writeFile request.
56875718 - version: v14.12.0
56885719 pr-url: https://github.com/nodejs/node/pull/34993
56895720 description: The `data` parameter will stringify an object with an
@@ -5700,6 +5731,7 @@ changes:
57005731 * ` encoding ` {string|null} ** Default:** ` 'utf8' `
57015732 * ` mode ` {integer} ** Default:** ` 0o666 `
57025733 * ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'w' ` .
5734+ * ` signal ` {AbortSignal} allows aborting an in-progress writeFile
57035735* Returns: {Promise}
57045736
57055737Asynchronously writes data to a file, replacing the file if it already exists.
@@ -5713,7 +5745,34 @@ If `options` is a string, then it specifies the encoding.
57135745Any specified ` FileHandle ` has to support writing.
57145746
57155747It is unsafe to use ` fsPromises.writeFile() ` multiple times on the same file
5716- without waiting for the ` Promise ` to be resolved (or rejected).
5748+ without waiting for the ` Promise ` to be fulfilled (or rejected).
5749+
5750+ Similarly to ` fsPromises.readFile ` - ` fsPromises.writeFile ` is a convenience
5751+ method that performs multiple ` write ` calls internally to write the buffer
5752+ passed to it. For performance sensitive code consider using
5753+ [ ` fs.createWriteStream() ` ] [ ] .
5754+
5755+ It is possible to use an {AbortSignal} to cancel an ` fsPromises.writeFile() ` .
5756+ Cancelation is "best effort", and some amount of data is likely still
5757+ to be written.
5758+
5759+ ``` js
5760+ const controller = new AbortController ();
5761+ const { signal } = controller;
5762+ const data = new Uint8Array (Buffer .from (' Hello Node.js' ));
5763+ (async () => {
5764+ try {
5765+ await fs .writeFile (' message.txt' , data, { signal });
5766+ } catch (err) {
5767+ // When a request is aborted - err is an AbortError
5768+ }
5769+ })();
5770+ // When the request should be aborted
5771+ controller .abort ();
5772+ ```
5773+
5774+ Aborting an ongoing request does not abort individual operating
5775+ system requests but rather the internal buffering ` fs.writeFile ` performs.
57175776
57185777## FS constants
57195778
0 commit comments