Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- // Function to read a file
- function readFile(filePath) {
- return new Promise((resolve, reject) => {
- fs.readFile(filePath, 'utf8', (err, data) => {
- if (err) {
- reject(err); // Reject the promise if there's an error
- } else {
- resolve(data); // Resolve the promise with the file data
- }
- });
- });
- }
- // Main function to execute the file reading
- async function main() {
- const filePath = 'example.txt'; // Change this to an invalid path to test error handling
- try {
- const data = await readFile(filePath);
- console.log('File Content:', data);
- } catch (error) {
- console.error('Error reading file:', error.message);
- }
- }
- // Execute the main function
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement