Bird
Raised Fist0
NextJSframework~20 mins

Async server components in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main benefit of using async server components in Next.js?
easy
A. They enable real-time updates without refreshing the page.
B. They allow client-side state management inside the component.
C. They automatically cache data on the client device.
D. They fetch data before rendering, sending ready HTML to the browser.

Solution

  1. Step 1: Understand async server components purpose

    Async server components fetch data on the server before rendering the page.
  2. Step 2: Identify the benefit of pre-fetching data

    This means the browser receives fully rendered HTML, improving speed and user experience.
  3. Final Answer:

    They fetch data before rendering, sending ready HTML to the browser. -> Option D
  4. Quick Check:

    Async server components = fetch data first [OK]
Hint: Async server components fetch data before rendering [OK]
Common Mistakes:
  • Thinking async components run on the client
  • Confusing caching with async data fetching
  • Assuming real-time updates happen automatically
2. Which of the following is the correct way to define an async server component in Next.js?
easy
A. async function MyComponent() { const data = await fetchData(); return
{data}
; }
B. function MyComponent() { const data = await fetchData(); return
{data}
; }
C. const MyComponent = () => { const data = await fetchData(); return
{data}
; }
D. const MyComponent = async () => { fetchData().then(data =>
{data}
); }

Solution

  1. Step 1: Identify async function syntax for components

    Next.js requires the component function to be marked async to use await inside.
  2. Step 2: Check each option for correct async usage

    async function MyComponent() { const data = await fetchData(); return
    {data}; } correctly defines an async function and uses await inside. function MyComponent() { const data = await fetchData(); return
    {data}
    ; } misses async keyword. const MyComponent = () => { const data = await fetchData(); return
    {data}
    ; } uses arrow function but misses async. const MyComponent = async () => { fetchData().then(data =>
    {data}
    ); } uses then() but does not return JSX properly.
  3. Final Answer:

    async function MyComponent() { const data = await fetchData(); return <div>{data}</div>; } -> Option A
  4. Quick Check:

    Async keyword + await inside = async function MyComponent() { const data = await fetchData(); return
    {data}
    ; } [OK]
Hint: Async components must be declared with async keyword [OK]
Common Mistakes:
  • Forgetting to add async keyword
  • Using await without async function
  • Returning JSX inside then() without return statement
3. Given this async server component code, what will be rendered?
async function Greeting() {
  const name = await Promise.resolve('Alice');
  return <h1>Hello, {name}!</h1>;
}
medium
A. <h1>Hello, Alice!</h1>
B. Hello, {name}!
C. Promise { 'Alice' }
D. Error: Cannot use await here

Solution

  1. Step 1: Understand async function behavior in server components

    The function awaits the resolved promise 'Alice' and stores it in name.
  2. Step 2: Render JSX with awaited value

    The component returns an h1 element with the text 'Hello, Alice!'. This HTML is sent to the browser.
  3. Final Answer:

    <h1>Hello, Alice!</h1> -> Option A
  4. Quick Check:

    Await resolves promise, renders value [OK]
Hint: Await resolves promise before rendering JSX [OK]
Common Mistakes:
  • Thinking JSX shows variable name literally
  • Expecting promise object to render
  • Assuming await is disallowed in server components
4. What is wrong with this async server component code?
async function UserProfile() {
  const user = fetch('/api/user');
  return <div>{user.name}</div>;
}
medium
A. Cannot use fetch inside server components.
B. Missing await before fetch call, so user is a promise, not data.
C. JSX cannot contain expressions like {user.name}.
D. Function should not be async if it returns JSX.

Solution

  1. Step 1: Check fetch usage in async function

    fetch returns a promise, so it must be awaited to get the data.
  2. Step 2: Identify the error in accessing user.name

    Without await, user is a promise object, so user.name is undefined causing error.
  3. Final Answer:

    Missing await before fetch call, so user is a promise, not data. -> Option B
  4. Quick Check:

    Always await fetch to get data [OK]
Hint: Always await fetch to get resolved data [OK]
Common Mistakes:
  • Forgetting await before fetch
  • Thinking fetch is disallowed in server components
  • Misunderstanding JSX expressions
5. You want to fetch user data and posts in an async server component and render them together. Which code correctly fetches both before rendering?
async function Dashboard() {
  const user = await fetch('/api/user').then(res => res.json());
  const posts = fetch('/api/posts').then(res => res.json());
  return (
    <div>
      <h1>{user.name}</h1>
      <ul>{posts.map(post => <li key={post.id}>{post.title}</li>)}
hard
A. Use then() only without await for both fetches.
B. Remove await before fetch('/api/user') to run both fetches in parallel.
C. Add await before fetch('/api/posts') to wait for posts data.
D. Return JSX before fetching posts to speed up rendering.

Solution

  1. Step 1: Identify missing await for posts fetch

    The posts fetch is missing await, so posts is a promise, not data.
  2. Step 2: Fix by adding await to posts fetch

    Adding await ensures posts data is ready before rendering the list.
  3. Final Answer:

    Add await before fetch('/api/posts') to wait for posts data. -> Option C
  4. Quick Check:

    Await all fetches before rendering [OK]
Hint: Await all fetches before using their data [OK]
Common Mistakes:
  • Forgetting await on second fetch
  • Removing await on first fetch causing errors
  • Returning JSX before data is ready
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard