0
0
Node.jsframework~20 mins

Reading files asynchronously with callbacks in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async File Reader Master
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 Node.js asynchronous file read?
Consider the following code snippet that reads a file asynchronously using callbacks. What will be printed to the console?
Node.js
import { readFile } from 'node:fs';

readFile('example.txt', 'utf8', (err, data) => {
  if (err) {
    console.log('Error:', err.message);
  } else {
    console.log('File content:', data);
  }
});

console.log('Read request sent');
AError: ENOENT: no such file or directory, open 'example.txt'\nRead request sent
BFile content: (contents of example.txt)\nRead request sent
CRead request sent\nFile content: (contents of example.txt)
DRead request sent
Attempts:
2 left
💡 Hint
Remember that readFile is asynchronous and the callback runs after the main thread continues.
component_behavior
intermediate
2:00remaining
What happens if the callback is missing in readFile?
What will happen if you call readFile without providing a callback function?
Node.js
import { readFile } from 'node:fs';

readFile('example.txt', 'utf8');
AA TypeError is thrown because callback is required.
BThe program crashes with an unhandled exception.
CNothing happens; the file is not read.
DThe file is read and data is returned synchronously.
Attempts:
2 left
💡 Hint
Check the Node.js documentation for readFile function signature.
🔧 Debug
advanced
2:00remaining
Why does this callback not print the file content?
This code tries to read a file and print its content but only prints 'Read request sent'. What is the issue?
Node.js
import { readFile } from 'node:fs';

readFile('example.txt', 'utf8', (err, data) => {
  if (err) {
    console.error(err);
  }
});

console.log('Read request sent');
AThe callback does not print the data when there is no error.
BThe file path is incorrect causing silent failure.
CThe encoding option 'utf8' is invalid causing no data.
DThe console.log outside the callback blocks the callback from running.
Attempts:
2 left
💡 Hint
Look at what the callback does when there is no error.
📝 Syntax
advanced
2:00remaining
Which option correctly reads a file asynchronously with a callback?
Select the code snippet that correctly reads 'data.txt' asynchronously and logs its content or error.
AreadFile('data.txt', (data, err) => { if (err) throw err; console.log(data); });
BreadFile('data.txt', (err, data) => { if (err) throw err; console.log(data); });
CreadFile('data.txt', 'utf8', (data, err) => { if (err) throw err; console.log(data); });
DreadFile('data.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data); });
Attempts:
2 left
💡 Hint
Check the order of parameters in the callback and the encoding argument.
state_output
expert
2:00remaining
What is the value of 'result' after this asynchronous readFile call?
Given this code, what will be the value of 'result' after execution?
Node.js
import { readFile } from 'node:fs';

let result = '';

readFile('file.txt', 'utf8', (err, data) => {
  if (!err) {
    result = data;
  }
});

console.log(result);
AThrows a ReferenceError.
BAn empty string.
Cundefined.
DThe content of 'file.txt' as a string.
Attempts:
2 left
💡 Hint
Remember that readFile is asynchronous and console.log runs before the callback.