@@ -4375,6 +4375,10 @@ details.
4375
4375
<!-- YAML
4376
4376
added: v0.1.29
4377
4377
changes:
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.
4378
4382
- version: v14.12.0
4379
4383
pr-url: https://github.com/nodejs/node/pull/34993
4380
4384
description: The `data` parameter will stringify an object with an
@@ -4409,6 +4413,7 @@ changes:
4409
4413
* ` encoding ` {string|null} ** Default:** ` 'utf8' `
4410
4414
* ` mode ` {integer} ** Default:** ` 0o666 `
4411
4415
* ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'w' ` .
4416
+ * ` signal ` {AbortSignal} allows aborting an in-progress writeFile
4412
4417
* ` callback ` {Function}
4413
4418
* ` err ` {Error}
4414
4419
@@ -4440,6 +4445,28 @@ It is unsafe to use `fs.writeFile()` multiple times on the same file without
4440
4445
waiting for the callback. For this scenario, [ ` fs.createWriteStream() ` ] [ ] is
4441
4446
recommended.
4442
4447
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
+
4443
4470
### Using ` fs.writeFile() ` with file descriptors
4444
4471
4445
4472
When ` file ` is a file descriptor, the behavior is almost identical to directly
@@ -5684,6 +5711,10 @@ The `atime` and `mtime` arguments follow these rules:
5684
5711
<!-- YAML
5685
5712
added: v10.0.0
5686
5713
changes:
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.
5687
5718
- version: v14.12.0
5688
5719
pr-url: https://github.com/nodejs/node/pull/34993
5689
5720
description: The `data` parameter will stringify an object with an
@@ -5700,6 +5731,7 @@ changes:
5700
5731
* ` encoding ` {string|null} ** Default:** ` 'utf8' `
5701
5732
* ` mode ` {integer} ** Default:** ` 0o666 `
5702
5733
* ` flag ` {string} See [ support of file system ` flags ` ] [ ] . ** Default:** ` 'w' ` .
5734
+ * ` signal ` {AbortSignal} allows aborting an in-progress writeFile
5703
5735
* Returns: {Promise}
5704
5736
5705
5737
Asynchronously 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.
5713
5745
Any specified ` FileHandle ` has to support writing.
5714
5746
5715
5747
It 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.
5717
5776
5718
5777
## FS constants
5719
5778
0 commit comments