diff --git a/assignment1/async-demo.js b/assignment1/async-demo.js index e8c480b..fa3aefa 100644 --- a/assignment1/async-demo.js +++ b/assignment1/async-demo.js @@ -1,16 +1,29 @@ -const fs = require('fs'); -const path = require('path'); +const fs = require('node:fs'); +const path = require('node:path'); +const CONTENT_STRING = 'Hello, async world!'; // Write a sample file for demonstration +const filePath = path.join(__dirname, 'sample-files', 'sample.txt'); -// 1. Callback style - - - // Callback hell example (test and leave it in comments): - +const main = async () => { + // 1. Callback style + console.log('Callback read:', CONTENT_STRING); + fs.writeFileSync(filePath, CONTENT_STRING); // 2. Promise style - - - // 3. Async/Await style + console.log('Promise read:', CONTENT_STRING); + fs.promises + .writeFile(filePath, CONTENT_STRING) + .catch((err) => console.error('Error writing file with promises:', err)); + + // 3. Async/Await style + try { + console.log('Async/Await read:', CONTENT_STRING); + await fs.promises.writeFile(filePath, CONTENT_STRING); + } catch (err) { + console.error('Error writing file with async/await:', err); + } +}; + +main(); diff --git a/assignment1/core-modules-demo.js b/assignment1/core-modules-demo.js index 16e9390..b8556f9 100644 --- a/assignment1/core-modules-demo.js +++ b/assignment1/core-modules-demo.js @@ -1,18 +1,42 @@ -const os = require('os'); -const path = require('path'); -const fs = require('fs'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); const sampleFilesDir = path.join(__dirname, 'sample-files'); if (!fs.existsSync(sampleFilesDir)) { fs.mkdirSync(sampleFilesDir, { recursive: true }); } -// OS module +const main = async () => { + // OS module + console.log(`Platform: ${os.platform()}`); + console.log(`CPU: ${os.cpus()[0].model}`); + console.log(`Total Memory: ${os.totalmem()}`); + const demoTextFilePath = path.join(sampleFilesDir, 'demo.txt'); + // Path module + console.log('Joined path:', demoTextFilePath); -// Path module + // fs.promises API + await fs.promises.writeFile(demoTextFilePath, 'Hello from fs.promises!'); + console.log('fs.promises read:', await fs.promises.readFile(demoTextFilePath, 'utf8')); -// fs.promises API + // Streams for large files- log first 40 chars of each chunk + // 1. Create a large file for demonstration + const largeFilePath = path.join(sampleFilesDir, 'largefile.txt'); + if (!fs.existsSync(largeFilePath)) { + const largeContent = 'A'.repeat(10 * 1024); // Lots of 'A' + fs.writeFileSync(largeFilePath, largeContent); + } + // 2. Read the large file using streams + const readStream = fs.createReadStream(largeFilePath, { encoding: 'utf8' }); + readStream.on('data', (chunk) => { + console.log('Read chunk:', chunk.slice(0, 40)); // Log first 40 chars of each chunk + }); + readStream.on('end', () => { + console.log('Finished reading large file with streams'); + }); +}; -// Streams for large files- log first 40 chars of each chunk +main(); diff --git a/assignment1/globals-demo.js b/assignment1/globals-demo.js index 6fef75e..0256634 100644 --- a/assignment1/globals-demo.js +++ b/assignment1/globals-demo.js @@ -1,7 +1,7 @@ -// Log __dirname and __filename +console.log('__dirname:', __dirname); +console.log('__filename:', __filename); +console.log('Process ID:', process.pid); +console.log('Platform:', process.platform); - -// Log process ID and platform - - -// Attach a custom property to global and log it +globalThis.myCustomVar = 'Hello, global!'; +console.log('Custom global variable:', globalThis.myCustomVar);