Challenge - 5 Problems
File Reading Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code reading a file?
Consider this Node.js code using fs.promises to read a file named 'example.txt'. What will be logged to the console?
Node.js
import { promises as fs } from 'fs'; async function readFile() { try { const data = await fs.readFile('example.txt', 'utf8'); console.log(data.trim()); } catch (error) { console.log('Error:', error.message); } } readFile();
Attempts:
2 left
💡 Hint
Check how fs.promises.readFile returns a promise and how await handles it.
✗ Incorrect
fs.promises.readFile returns a promise that resolves with the file content as a string when 'utf8' encoding is specified. The await keyword waits for this promise, so console.log prints the trimmed file content.
❓ component_behavior
intermediate2:00remaining
What happens if you forget to await fs.promises.readFile?
In this code snippet, what will be logged to the console?
Node.js
import { promises as fs } from 'fs'; async function readFile() { try { const data = fs.readFile('example.txt', 'utf8'); console.log(typeof data); } catch (error) { console.log('Error:', error.message); } } readFile();
Attempts:
2 left
💡 Hint
Remember what fs.promises.readFile returns when not awaited.
✗ Incorrect
Without await, fs.readFile returns a Promise object. Logging typeof data prints 'object'. The file content is not yet available.
📝 Syntax
advanced2:00remaining
Which option correctly reads a file using fs.promises with async/await?
Select the code snippet that correctly reads 'data.txt' and logs its content.
Attempts:
2 left
💡 Hint
Check the import style and usage of async/await with fs.promises.
✗ Incorrect
Option C correctly imports fs.promises as fs and uses await to read the file with encoding 'utf8'. Option C uses require which is valid in CommonJS but the prompt targets ES modules. Option C omits the 'utf8' encoding parameter, so content is a Buffer. Option C uses synchronous readFileSync inside a non-async function.
🔧 Debug
advanced2:00remaining
Why does this code throw an error when reading a file?
This code throws an error. What is the cause?
Node.js
import { promises as fs } from 'fs'; async function read() { const data = await fs.readFile('missing.txt'); console.log(data); } read();
Attempts:
2 left
💡 Hint
What happens if you try to read a file that does not exist without error handling?
✗ Incorrect
If the file does not exist, fs.promises.readFile rejects the promise. Without a try/catch, the rejection causes an unhandled promise rejection error.
🧠 Conceptual
expert2:00remaining
What is the advantage of using fs.promises over callback-based fs methods?
Choose the best explanation for why developers prefer fs.promises for reading files.
Attempts:
2 left
💡 Hint
Think about how promises improve asynchronous code structure.
✗ Incorrect
fs.promises returns promises, enabling async/await syntax which simplifies asynchronous code and error handling compared to nested callbacks.