Question: Using Javascript Need question one answered. The results are displayed in the terminal and the test code (what the answer should look like) It is

Using JavascriptUsing Javascript Need question one answered. The results are displayed in the

Need question one answered. The results are displayed in the terminal and the test code (what the answer should look like) It is a simple program. The objective of this assignment is to learn about ansychronous functions in javascript. I need a write data function and a read data function using fs. The write data should write to the text file. I need to have this function to save from a file and load from a file. Here is an example we used in class and the example should look similar. The results of the text file should not be displayed in the first question because it uses ansych method but it should be displayed in the second.

const fs = require('fs')

console.log("Before Synch read")

var data = fs.readFileSync('test.txt',{encoding: 'utf-8'})

console.log(data)

console.log("After synch read")

console.log("Before Asynch read")

var data2 = fs.readFile('test.txt',{encoding:'utf-8'},function(err,data2){

console.log(data2)

})

console.log("After Asynch Read")

(Write code and capture the result in terminal) 1. Make writeData function and readData function using fs asynchronous methods (fs.write File and fs.readFile). Use the function to save file and load data writeData('./data1.txt', 'this is datal') writeData('./data2.txt', 'this is data2') writeData("./data3.txt', 'this is data3') Run var readData1 = readData("./data1.txt') console.log("Load ./data1.txt") console.log(readData1) var readData2 =readData("./data2.txt') console.log("Load ./data2.txt") console.log(readData2) var readData3 =readData("./data3.txt') console.log("Load ./data3.txt") console.log(readData3) code\Homework> node '. \Homew ork 2 - fs (answer).js' Load ./data1.txt undefined Load ./data2.txt undefined Load ./data3.txt undefined Result in terminal 2. If you make asynchronous read file function, the function must use Promise to display result. Fix your functions (Promise) and modify the test code (await) to read files. Like: Result in terminal. Load ./data1.txt this is data1 Load ./data2.txt this is data2 Load ./data3.txt this is data3 Result in terminal (Write code and capture the result in terminal) 1. Make writeData function and readData function using fs asynchronous methods (fs.write File and fs.readFile). Use the function to save file and load data writeData('./data1.txt', 'this is datal') writeData('./data2.txt', 'this is data2') writeData("./data3.txt', 'this is data3') Run var readData1 = readData("./data1.txt') console.log("Load ./data1.txt") console.log(readData1) var readData2 =readData("./data2.txt') console.log("Load ./data2.txt") console.log(readData2) var readData3 =readData("./data3.txt') console.log("Load ./data3.txt") console.log(readData3) code\Homework> node '. \Homew ork 2 - fs (answer).js' Load ./data1.txt undefined Load ./data2.txt undefined Load ./data3.txt undefined Result in terminal 2. If you make asynchronous read file function, the function must use Promise to display result. Fix your functions (Promise) and modify the test code (await) to read files. Like: Result in terminal. Load ./data1.txt this is data1 Load ./data2.txt this is data2 Load ./data3.txt this is data3 Result in terminal

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!