Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions assignment1/async-demo.js
Original file line number Diff line number Diff line change
@@ -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();
38 changes: 31 additions & 7 deletions assignment1/core-modules-demo.js
Original file line number Diff line number Diff line change
@@ -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();
12 changes: 6 additions & 6 deletions assignment1/globals-demo.js
Original file line number Diff line number Diff line change
@@ -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);