0
0
Node.jsframework~10 mins

Reading files with promises (fs.promises) in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the promises API from the fs module.

Node.js
import { [1] } from 'fs';
Drag options to blanks, or click blank then click option'
Apromises
BreadFile
CwriteFile
DcreateReadStream
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'readFile' directly instead of from 'fs.promises'.
Using 'createReadStream' which is not promise-based.
2fill in blank
medium

Complete the code to read a file named 'data.txt' using promises.

Node.js
const data = await promises.[1]('data.txt', 'utf-8');
Drag options to blanks, or click blank then click option'
AwriteFile
BappendFile
CreadFile
Dunlink
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'writeFile' which writes data instead of reading.
Using 'appendFile' which adds data to a file.
3fill in blank
hard

Fix the error in the async function to correctly read 'info.txt'.

Node.js
async function readInfo() {
  const content = await promises.[1]('info.txt', 'utf-8');
  return content;
}
Drag options to blanks, or click blank then click option'
AreadStream
BreadFileSync
Cread
DreadFile
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'readFileSync' inside an async function with await causes errors.
Using non-existent methods like 'readStream'.
4fill in blank
hard

Fill both blanks to handle errors when reading 'notes.txt' with promises.

Node.js
try {
  const notes = await promises.[1]('notes.txt', 'utf-8');
  console.log(notes);
} catch ([2]) {
  console.error('Error reading file:', err);
}
Drag options to blanks, or click blank then click option'
AreadFile
BwriteFile
Cerror
Derr
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'writeFile' instead of 'readFile' to read files.
Not naming the error parameter in catch block.
5fill in blank
hard

Fill all three blanks to create an async function that reads 'log.txt' and returns its content or an error message.

Node.js
async function readLog() {
  try {
    const content = await promises.[1]('log.txt', [2]);
    return content;
  } catch ([3]) {
    return 'Failed to read file';
  }
}
Drag options to blanks, or click blank then click option'
AreadFile
B'utf-8'
Cerror
DwriteFile
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'writeFile' instead of 'readFile'.
Omitting the encoding string or using wrong quotes.
Not naming the error parameter in catch.