File tree Expand file tree Collapse file tree 1 file changed +20
-4
lines changed Expand file tree Collapse file tree 1 file changed +20
-4
lines changed Original file line number Diff line number Diff line change @@ -3562,8 +3562,16 @@ console.log(fs.readFileSync('temp.txt', 'utf8'));
3562
3562
// Prints: Node.js
3563
3563
3564
3564
async function doTruncate () {
3565
- const fd = await fsPromises .open (' temp.txt' , ' r+' );
3566
- await fsPromises .ftruncate (fd, 4 );
3565
+ let filehandle = null ;
3566
+ try {
3567
+ filehandle = await fsPromises .open (' temp.txt' , ' r+' );
3568
+ await filehandle .truncate (4 );
3569
+ } finally {
3570
+ if (filehandle) {
3571
+ // close the file if it is opened.
3572
+ await filehandle .close ();
3573
+ }
3574
+ }
3567
3575
console .log (fs .readFileSync (' temp.txt' , ' utf8' )); // Prints: Node
3568
3576
}
3569
3577
@@ -3581,8 +3589,16 @@ console.log(fs.readFileSync('temp.txt', 'utf8'));
3581
3589
// Prints: Node.js
3582
3590
3583
3591
async function doTruncate () {
3584
- const fd = await fsPromises .open (' temp.txt' , ' r+' );
3585
- await fsPromises .ftruncate (fd, 10 );
3592
+ let filehandle = null ;
3593
+ try {
3594
+ filehandle = await fsPromises .open (' temp.txt' , ' r+' );
3595
+ await filehandle .truncate (10 );
3596
+ } finally {
3597
+ if (filehandle) {
3598
+ // close the file if it is opened.
3599
+ await filehandle .close ();
3600
+ }
3601
+ }
3586
3602
console .log (fs .readFileSync (' temp.txt' , ' utf8' )); // Prints Node.js\0\0\0
3587
3603
}
3588
3604
You can’t perform that action at this time.
0 commit comments