0
0
NextJSframework~20 mins

Async server components in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Server Components Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this async server component render?
Consider this Next.js async server component. What will it render on the page?
NextJS
import React from 'react';

async function fetchData() {
  return new Promise(resolve => setTimeout(() => resolve('Hello from server!'), 100));
}

export default async function Greeting() {
  const message = await fetchData();
  return <div>{message}</div>;
}
A<div>Hello from server!</div>
BComponent throws an error
C<div>undefined</div>
D<div>Promise {<pending>}</div>
Attempts:
2 left
💡 Hint
Remember that async server components can await promises before rendering.
lifecycle
intermediate
2:00remaining
When does this async server component fetch data?
Given this async server component, when is fetchData called?
NextJS
async function fetchData() {
  console.log('Fetching data');
  return 'Data';
}

export default async function DataComponent() {
  const data = await fetchData();
  return <p>{data}</p>;
}
AOnly once when the app starts in the browser
BAt build time or on each server request before rendering
CEvery time the user clicks on the component
DOnly after the component mounts on the client
Attempts:
2 left
💡 Hint
Async server components run on the server before sending HTML.
📝 Syntax
advanced
2:00remaining
Which option correctly defines an async server component in Next.js?
Select the correct syntax for an async server component that fetches data and renders it.
A
export default async function MyComponent() {
  const data = await fetch('/api/data');
  return &lt;div&gt;{data.json()}&lt;/div&gt;;
}
B
export default function MyComponent() {
  const data = await fetch('/api/data');
  return &lt;div&gt;{data}&lt;/div&gt;;
}
C
export default function MyComponent() {
  const data = fetch('/api/data');
  return &lt;div&gt;{data}&lt;/div&gt;;
}
D
export default async function MyComponent() {
  const res = await fetch('/api/data');
  const data = await res.json();
  return &lt;div&gt;{data.message}&lt;/div&gt;;
}
Attempts:
2 left
💡 Hint
Remember to await fetch and then await the response.json() in async functions.
🔧 Debug
advanced
2:00remaining
Why does this async server component cause a runtime error?
Identify the cause of the runtime error in this async server component.
NextJS
export default async function BrokenComponent() {
  const data = fetch('https://api.example.com/data');
  return <div>{data}</div>;
}
ABecause fetch is not awaited, data is a Promise which cannot be rendered causing error
BBecause fetch is awaited but response is not parsed as JSON
CBecause the API URL is invalid
DBecause async functions cannot return JSX directly
Attempts:
2 left
💡 Hint
Check if fetch is awaited before accessing its result.
🧠 Conceptual
expert
2:00remaining
What is a key benefit of using async server components in Next.js?
Choose the most accurate benefit of async server components compared to client components.
AThey automatically cache data on the client for offline use
BThey run in the browser and improve client-side interactivity
CThey allow fetching data on the server before sending HTML, improving performance and SEO
DThey replace the need for API routes entirely
Attempts:
2 left
💡 Hint
Think about where async server components run and how that affects loading.