From 1df3b4ec78687fb51e7660aef06da4d7e50f673d Mon Sep 17 00:00:00 2001 From: Beth Griggs Date: Tue, 19 May 2020 12:38:09 +0100 Subject: [PATCH] Chapter 2: edits --- .../working-with-files/readWriteAsync.js | 7 +++--- .../working-with-files/readWriteAsync2.js | 9 ++------ .../working-with-files/readWriteAsyncNamed.js | 21 +++++++++++++++++ .../readWriteAsyncTimeout.js | 23 +++++++++++++++++++ Chapter02/working-with-files/readWriteSync.js | 1 + 5 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 Chapter02/working-with-files/readWriteAsyncNamed.js create mode 100644 Chapter02/working-with-files/readWriteAsyncTimeout.js diff --git a/Chapter02/working-with-files/readWriteAsync.js b/Chapter02/working-with-files/readWriteAsync.js index 12cdc56..f4570c7 100644 --- a/Chapter02/working-with-files/readWriteAsync.js +++ b/Chapter02/working-with-files/readWriteAsync.js @@ -9,8 +9,7 @@ fs.readFile(filepath, "utf8", function (err, contents) { } console.log("File Contents:", contents); const upperContents = contents.toUpperCase(); - fs.writeFile(filepath, upperContents, function (err) { - if (err) throw err; - console.log("File updated."); - }); + + fs.writeFileSync(filepath, upperContents); + console.log("File updated."); }); diff --git a/Chapter02/working-with-files/readWriteAsync2.js b/Chapter02/working-with-files/readWriteAsync2.js index 3b65a1d..bc421b0 100644 --- a/Chapter02/working-with-files/readWriteAsync2.js +++ b/Chapter02/working-with-files/readWriteAsync2.js @@ -9,14 +9,9 @@ fs.readFile(filepath, "utf8", function (err, contents) { } console.log("File Contents:", contents); const upperContents = contents.toUpperCase(); - setTimeout(() => updateFile(filepath, upperContents), 10); -}); -function updateFile(filepath, contents) { - fs.writeFile(filepath, contents, function (err) { + fs.writeFile(filepath, upperContents, function (err) { if (err) throw err; console.log("File updated."); }); -} - -setInterval(() => process.stdout.write("**** \n"), 1).unref(); +}); diff --git a/Chapter02/working-with-files/readWriteAsyncNamed.js b/Chapter02/working-with-files/readWriteAsyncNamed.js new file mode 100644 index 0000000..6b4fa5e --- /dev/null +++ b/Chapter02/working-with-files/readWriteAsyncNamed.js @@ -0,0 +1,21 @@ +const fs = require("fs"); +const path = require("path"); + +const filepath = path.join(process.cwd(), "hello.txt"); + +fs.readFile(filepath, "utf8", function (err, contents) { + if (err) { + return console.log(err); + } + console.log("File Contents:", contents); + + const upperContents = contents.toUpperCase(); + updateFile(filepath, upperContents); +}); + +function updateFile(filepath, contents) { + fs.writeFile(filepath, contents, function (err) { + if (err) throw err; + console.log("File updated."); + }); +} diff --git a/Chapter02/working-with-files/readWriteAsyncTimeout.js b/Chapter02/working-with-files/readWriteAsyncTimeout.js new file mode 100644 index 0000000..c06a8e2 --- /dev/null +++ b/Chapter02/working-with-files/readWriteAsyncTimeout.js @@ -0,0 +1,23 @@ +const fs = require("fs"); +const path = require("path"); + +const filepath = path.join(process.cwd(), "hello.txt"); + +fs.readFile(filepath, "utf8", function (err, contents) { + if (err) { + return console.log(err); + } + console.log("File Contents:", contents); + const upperContents = contents.toUpperCase(); + + setTimeout(() => updateFile(filepath, upperContents), 10); +}); + +function updateFile(filepath, contents) { + fs.writeFile(filepath, contents, function (err) { + if (err) throw err; + console.log("File updated."); + }); +} + +setInterval(() => process.stdout.write("**** \n"), 1).unref(); diff --git a/Chapter02/working-with-files/readWriteSync.js b/Chapter02/working-with-files/readWriteSync.js index 811d01d..6b15987 100644 --- a/Chapter02/working-with-files/readWriteSync.js +++ b/Chapter02/working-with-files/readWriteSync.js @@ -7,5 +7,6 @@ const contents = fs.readFileSync(filepath, "utf8"); console.log("File Contents:", contents); const upperContents = contents.toUpperCase(); + fs.writeFileSync(filepath, upperContents); console.log("File updated.");