Complete the code to import the promises API from the fs module.
import { [1] } from 'fs';
The promises object from the fs module provides promise-based file system methods.
Complete the code to read a file named 'data.txt' using promises.
const data = await promises.[1]('data.txt', 'utf-8');
The readFile method reads the contents of a file and returns a promise.
Fix the error in the async function to correctly read 'info.txt'.
async function readInfo() {
const content = await promises.[1]('info.txt', 'utf-8');
return content;
}readFile is the correct async method returning a promise. readFileSync is synchronous and not used with await.
Fill both blanks to handle errors when reading 'notes.txt' with promises.
try { const notes = await promises.[1]('notes.txt', 'utf-8'); console.log(notes); } catch ([2]) { console.error('Error reading file:', err); }
Use readFile to read the file and catch the error with a variable like err to handle exceptions.
Fill all three blanks to create an async function that reads 'log.txt' and returns its content or an error message.
async function readLog() {
try {
const content = await promises.[1]('log.txt', [2]);
return content;
} catch ([3]) {
return 'Failed to read file';
}
}The function uses readFile with encoding 'utf-8' and catches errors with the variable error.