Skip to content

Commit

Permalink
doc: fix filehandle.truncate() sample codes
Browse files Browse the repository at this point in the history
PR-URL: #20913
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Vse Mozhet Byt <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
Masashi Hirano authored and targos committed May 25, 2018
1 parent b242248 commit 1f3eb1c
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3562,8 +3562,16 @@ console.log(fs.readFileSync('temp.txt', 'utf8'));
// Prints: Node.js

async function doTruncate() {
const fd = await fsPromises.open('temp.txt', 'r+');
await fsPromises.ftruncate(fd, 4);
let filehandle = null;
try {
filehandle = await fsPromises.open('temp.txt', 'r+');
await filehandle.truncate(4);
} finally {
if (filehandle) {
// close the file if it is opened.
await filehandle.close();
}
}
console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node
}

Expand All @@ -3581,8 +3589,16 @@ console.log(fs.readFileSync('temp.txt', 'utf8'));
// Prints: Node.js

async function doTruncate() {
const fd = await fsPromises.open('temp.txt', 'r+');
await fsPromises.ftruncate(fd, 10);
let filehandle = null;
try {
filehandle = await fsPromises.open('temp.txt', 'r+');
await filehandle.truncate(10);
} finally {
if (filehandle) {
// close the file if it is opened.
await filehandle.close();
}
}
console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints Node.js\0\0\0
}

Expand Down

0 comments on commit 1f3eb1c

Please sign in to comment.