@@ -4385,6 +4385,10 @@ details.
4385
4385
<!-- YAML
4386
4386
added: v0.1.29
4387
4387
changes:
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.
4388
4392
- version: v14.12.0
4389
4393
pr-url: https://github.com/nodejs/node/pull/34993
4390
4394
description: The `data` parameter will stringify an object with an
@@ -4419,6 +4423,7 @@ changes:
4419
4423
* ` encoding ` {string|null} ** Default:** ` 'utf8' `
4420
4424
* ` mode ` {integer} ** Default:** ` 0o666 `
4421
4425
* ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'w' ` .
4426
+ * ` signal ` {AbortSignal} allows aborting an in-progress writeFile
4422
4427
* ` callback ` {Function}
4423
4428
* ` err ` {Error}
4424
4429
@@ -4450,6 +4455,28 @@ It is unsafe to use `fs.writeFile()` multiple times on the same file without
4450
4455
waiting for the callback. For this scenario, [ ` fs.createWriteStream() ` ] [ ] is
4451
4456
recommended.
4452
4457
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
+
4453
4480
### Using ` fs.writeFile() ` with file descriptors
4454
4481
4455
4482
When ` file ` is a file descriptor, the behavior is almost identical to directly
@@ -5717,6 +5744,10 @@ The `atime` and `mtime` arguments follow these rules:
5717
5744
<!-- YAML
5718
5745
added: v10.0.0
5719
5746
changes:
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.
5720
5751
- version: v14.12.0
5721
5752
pr-url: https://github.com/nodejs/node/pull/34993
5722
5753
description: The `data` parameter will stringify an object with an
@@ -5733,6 +5764,7 @@ changes:
5733
5764
* ` encoding ` {string|null} ** Default:** ` 'utf8' `
5734
5765
* ` mode ` {integer} ** Default:** ` 0o666 `
5735
5766
* ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'w' ` .
5767
+ * ` signal ` {AbortSignal} allows aborting an in-progress writeFile
5736
5768
* Returns: {Promise}
5737
5769
5738
5770
Asynchronously 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.
5746
5778
Any specified ` FileHandle ` has to support writing.
5747
5779
5748
5780
It 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.
5750
5809
5751
5810
## FS constants
5752
5811
0 commit comments