0
0
Node.jsframework~10 mins

Why async patterns are critical in Node.js in Node.js - Test Your Understanding

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

Complete the code to read a file asynchronously using Node.js built-in module.

Node.js
import { readFile } from 'fs/promises';

async function readData() {
  const data = await readFile('[1]', 'utf8');
  console.log(data);
}

readData();
Drag options to blanks, or click blank then click option'
A'./data.txt'
B'data.txt'
C'./data.json'
D'data.json'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting './' causes file not found errors.
Using synchronous readFileSync instead of async readFile.
2fill in blank
medium

Complete the code to handle an asynchronous operation with a callback in Node.js.

Node.js
import { readFile } from 'fs';

readFile('[1]', 'utf8', (err, data) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(data);
});
Drag options to blanks, or click blank then click option'
A'file.json'
B'./file.txt'
C'./file.json'
D'file.txt'
Attempts:
3 left
💡 Hint
Common Mistakes
Using synchronous readFileSync instead of async readFile.
Incorrect file path causing errors.
3fill in blank
hard

Fix the error in the async function to properly catch errors using try-catch.

Node.js
import { readFile } from 'fs/promises';

async function loadFile() {
  [1] {
    const content = await readFile('./info.txt', 'utf8');
    console.log(content);
  } catch (error) {
    console.error('Error:', error);
  }
}

loadFile();
Drag options to blanks, or click blank then click option'
Afinally
Bcatch
Ctry
Dawait
Attempts:
3 left
💡 Hint
Common Mistakes
Using catch without try causes syntax errors.
Not handling errors leads to unhandled promise rejections.
4fill in blank
hard

Fill both blanks to create a Promise that resolves after a delay and use async/await to wait for it.

Node.js
function delay(ms) {
  return new Promise(([1], reject) => {
    setTimeout(() => [2](), ms);
  });
}

async function run() {
  console.log('Start');
  await delay(1000);
  console.log('End after 1 second');
}

run();
Drag options to blanks, or click blank then click option'
Aresolve
Breject
Cresolve()
Dreject()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling reject() instead of resolve() causes the Promise to fail.
Not calling resolve() leaves the Promise pending forever.
5fill in blank
hard

Fill all three blanks to create an async function that fetches JSON data and handles errors.

Node.js
import fetch from 'node-fetch';

async function getData(url) {
  try {
    const response = await fetch([1]);
    if (!response.ok) throw new Error('Network error');
    const data = await response.[2]();
    return [3];
  } catch (err) {
    console.error('Fetch failed:', err);
  }
}
Drag options to blanks, or click blank then click option'
Aurl
Bjson
Cdata
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using response.text() instead of response.json() for JSON data.
Not returning the parsed data from the function.