0
0
Node.jsframework~10 mins

Async/await syntax 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 declare an asynchronous function.

Node.js
async function fetchData() {
  const data = await fetch([1]);
  return data.json();
}
Drag options to blanks, or click blank then click option'
Aasync 'https://api.example.com/data'
Bfetch('https://api.example.com/data')
Cawait 'https://api.example.com/data'
D'https://api.example.com/data'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting fetch inside the blank instead of just the URL string.
Using await inside the argument of fetch.
Not using quotes around the URL string.
2fill in blank
medium

Complete the code to wait for the promise result inside an async function.

Node.js
async function getUser() {
  const user = [1] fetchUserFromDB();
  return user;
}
Drag options to blanks, or click blank then click option'
Aasync
BfetchUserFromDB()
Cawait
Dthen
Attempts:
3 left
💡 Hint
Common Mistakes
Using then instead of await inside async functions.
Calling the function without await and expecting the resolved value.
3fill in blank
hard

Fix the error in the async function declaration.

Node.js
function [1] fetchData() {
  const response = await fetch('https://api.example.com');
  return response.json();
}
Drag options to blanks, or click blank then click option'
Aasync
Bawait
Casync function*
Dfunction*
Attempts:
3 left
💡 Hint
Common Mistakes
Placing await before the function keyword.
Using generator function syntax function* instead of async.
4fill in blank
hard

Fill both blanks to correctly handle errors with try/catch in async function.

Node.js
async function loadData() {
  try {
    const data = [1] fetchData();
    return data;
  } catch ([2]) {
    console.error('Error:', error);
  }
}
Drag options to blanks, or click blank then click option'
Aawait
Berr
Cerror
DfetchData
Attempts:
3 left
💡 Hint
Common Mistakes
Not using await inside try block.
Using a wrong variable name in catch parameter.
5fill in blank
hard

Fill all three blanks to create an async arrow function that fetches JSON data.

Node.js
const fetchJson = async () => {
  const response = await fetch([1]);
  const data = await response.[2]();
  return [3];
};
Drag options to blanks, or click blank then click option'
A'https://api.example.com/data'
Bjson
Cdata
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using text() instead of json() to parse response.
Returning the response object instead of parsed data.