NodeJS Slip20a Solution

const fs = require('fs');

// Define the file path
const filePath = 'example.txt';

// Create an event handler function
const fileEventHandler = (filePath) => {
// Open the file for reading
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the file:', err);
} else {
console.log('File content:');
console.log(data);
}
});
};

// Listen for a custom event 'readFileEvent'
process.on('readFileEvent', () => {
// Call the event handler function
fileEventHandler(filePath);
});

// Emit the 'readFileEvent' event
process.emit('readFileEvent');

No comments:

Post a Comment