Permalink
Browse files

doc: fix filehandle.truncate() sample codes

PR-URL: #20913
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information...
shisama authored and vsemozhetbyt committed May 23, 2018
1 parent 6caecc6 commit ac8226115e2192a7a46ba07789fa5136f74223e1
Showing with 20 additions and 4 deletions.
  1. +20 −4 doc/api/fs.md
View
@@ -3572,8 +3572,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
}
@@ -3591,8 +3599,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
}

0 comments on commit ac82261

Please sign in to comment.