From dae13a1b9113757f4dd745ae8004f634d915667a Mon Sep 17 00:00:00 2001 From: Komal Shehzadi Date: Sun, 15 Aug 2021 03:27:33 +0500 Subject: [PATCH] sync and async code --- 1-node-farm/starter/index.js | 31 ++++++++++++++++++++++++++++++ 1-node-farm/starter/txt/final.txt | 3 ++- 1-node-farm/starter/txt/output.txt | 2 ++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 1-node-farm/starter/index.js create mode 100644 1-node-farm/starter/txt/output.txt diff --git a/1-node-farm/starter/index.js b/1-node-farm/starter/index.js new file mode 100644 index 0000000000..9e9c8afa3d --- /dev/null +++ b/1-node-farm/starter/index.js @@ -0,0 +1,31 @@ +const fs = require('fs'); + +// Sync way, blocking +const textIn = fs.readFileSync('./txt/input.txt', 'utf-8', (err, data)=>{ + console.log(data); +}); + +console.log(textIn) + +const textOut = `this is what we know about the avacado: ${textIn}.\n created on ${Date.now()}`; + +fs.writeFileSync('./txt/output.txt', textOut); +console.log('File Written!'); + + +// Async way, non-blocking + + +fs.readFile('./txt/start.txt', 'utf-8', (err, data1)=>{ + fs.readFile(`./txt/${data1}.txt`, 'utf-8', (err, data2)=>{ + console.log(data2); + fs.readFile(`./txt/append.txt`, 'utf-8', (err, data3)=>{ + console.log(data3); + fs.writeFile('./txt/final.txt',`${data2}\n${data3}`, 'utf-8', err=>{ + console.log('your file has been written !'); + }); + }); + }); +}); + +console.log(`Reading file....`) diff --git a/1-node-farm/starter/txt/final.txt b/1-node-farm/starter/txt/final.txt index 01ff620388..6c03c74da2 100644 --- a/1-node-farm/starter/txt/final.txt +++ b/1-node-farm/starter/txt/final.txt @@ -1 +1,2 @@ -The avocado 🥑 is also used as the base for the Mexican dip known as guacamole, as well as a spread on corn tortillas or toast, served with spices. APPENDIX: Generally, avocados 🥑 are served raw, but some cultivars can be cooked for a short time without becoming bitter. \ No newline at end of file +The avocado 🥑 is also used as the base for the Mexican dip known as guacamole, as well as a spread on corn tortillas or toast, served with spices. +APPENDIX: Generally, avocados 🥑 are served raw, but some cultivars can be cooked for a short time without becoming bitter. \ No newline at end of file diff --git a/1-node-farm/starter/txt/output.txt b/1-node-farm/starter/txt/output.txt new file mode 100644 index 0000000000..1a7ad817a7 --- /dev/null +++ b/1-node-farm/starter/txt/output.txt @@ -0,0 +1,2 @@ +this is what we know about the avacado: The avocado 🥑 is popular in vegetarian cuisine as a substitute for meats in sandwiches and salads because of its high fat content 😄. + created on 1628979969797 \ No newline at end of file