From 1f3eb1cc1ec74b514f6e54fa88537818472bcb4d Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Wed, 23 May 2018 22:48:47 +0900 Subject: [PATCH] doc: fix filehandle.truncate() sample codes PR-URL: https://github.com/nodejs/node/pull/20913 Reviewed-By: James M Snell Reviewed-By: Benjamin Gruenbaum Reviewed-By: Vse Mozhet Byt Reviewed-By: Ruben Bridgewater Reviewed-By: Rich Trott --- doc/api/fs.md | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 957d6b4265ab7e..7905cd170c7de6 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -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 } @@ -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 }