0
0
Node.jsframework~20 mins

Reading files with promises (fs.promises) in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Reading Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
ASyntaxError due to missing callback in readFile
BThe trimmed content of 'example.txt' is printed.
Cundefined
DError: ENOENT: no such file or directory, open 'example.txt'
Attempts:
2 left
💡 Hint
Check how fs.promises.readFile returns a promise and how await handles it.
component_behavior
intermediate
2: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();
ASyntaxError due to missing await keyword
B'string' is logged because readFile returns file content directly.
CError: Cannot read property 'trim' of undefined
D'object' is logged because data is a Promise object.
Attempts:
2 left
💡 Hint
Remember what fs.promises.readFile returns when not awaited.
📝 Syntax
advanced
2: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.
A
const fs = require('fs').promises;
async function read() {
  const content = await fs.readFile('data.txt', 'utf8');
  console.log(content);
}
read();
B
import fs from 'fs';
async function read() {
  const content = await fs.promises.readFile('data.txt');
  console.log(content.toString());
}
read();
C
import { promises as fs } from 'fs';
async function read() {
  const content = await fs.readFile('data.txt', 'utf8');
  console.log(content);
}
read();
D
import fs from 'fs/promises';
function read() {
  const content = fs.readFileSync('data.txt', 'utf8');
  console.log(content);
}
read();
Attempts:
2 left
💡 Hint
Check the import style and usage of async/await with fs.promises.
🔧 Debug
advanced
2: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();
AThe file 'missing.txt' does not exist, causing a rejection that is not caught.
Bawait cannot be used outside a try/catch block.
CMissing encoding argument causes a SyntaxError.
Dfs.readFile returns undefined without encoding.
Attempts:
2 left
💡 Hint
What happens if you try to read a file that does not exist without error handling?
🧠 Conceptual
expert
2: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.
Afs.promises allows using async/await syntax, making asynchronous code easier to read and maintain.
Bfs.promises automatically retries reading files on failure without extra code.
Cfs.promises reads files synchronously, improving performance.
Dfs.promises requires less memory because it streams files by default.
Attempts:
2 left
💡 Hint
Think about how promises improve asynchronous code structure.