What if you could build and test your app without waiting for any server at all?
Why Mocking data fetching in NextJS? - Purpose & Use Cases
Imagine building a web app that shows user profiles, but the real server is slow or not ready yet. You try to test your pages, but you have to wait for real data every time or guess what the server will send.
Manually waiting for real servers makes development slow and frustrating. You might get errors if the server is down or data changes unexpectedly. It's hard to test how your app behaves with different data or errors.
Mocking data fetching lets you pretend to get data instantly without a real server. You can control what data your app sees, test different cases easily, and build your UI faster and safer.
fetch('/api/users').then(res => res.json()).then(data => render(data))const mockData = [{ id: 1, name: 'Alice' }]; render(mockData)Mocking data fetching makes your development smooth and predictable by simulating server responses instantly.
When building a Next.js blog, you can mock posts data to design the page layout before the backend is ready, avoiding delays and surprises.
Manual data fetching depends on real servers and can slow you down.
Mocking lets you simulate data instantly and control test cases.
This speeds up development and improves reliability.
Practice
Solution
Step 1: Understand mocking concept
Mocking means pretending to get data, so you don't rely on a real server.Step 2: Identify purpose in Next.js
This helps build and test UI without waiting for real backend responses.Final Answer:
To simulate data retrieval without needing a real server -> Option DQuick Check:
Mocking = Simulate data fetching [OK]
- Thinking mocking speeds up production
- Confusing mocking with data storage
- Assuming mocking encrypts data
Solution
Step 1: Check async function syntax
async function fetchData() { return new Promise(resolve => setTimeout(() => resolve({ data: [] }), 1000)); } correctly returns a Promise that resolves after 1 second with data.Step 2: Identify incorrect patterns
const fetchData = () => { return { data: [] }; } is synchronous, C does not return a Promise, D returns timeout ID, not data.Final Answer:
async function fetchData() { return new Promise(resolve => setTimeout(() => resolve({ data: [] }), 1000)); } -> Option AQuick Check:
Async + Promise + delay = async function fetchData() { return new Promise(resolve => setTimeout(() => resolve({ data: [] }), 1000)); } [OK]
- Returning data synchronously without Promise
- Using setTimeout without Promise
- Returning timeout ID instead of data
async function fetchData() {
return new Promise(resolve => setTimeout(() => resolve({ users: ["Alice", "Bob"] }), 500));
}
fetchData().then(data => console.log(data.users));What will be logged to the console?
Solution
Step 1: Understand the Promise resolution
The function returns a Promise that resolves after 500ms with an object containing users array.Step 2: Check the then callback
The then callback logs data.users, which is ["Alice", "Bob"].Final Answer:
["Alice", "Bob"] -> Option CQuick Check:
Promise resolves to users array logged [OK]
- Expecting immediate data without waiting
- Logging the Promise object instead of resolved value
- Confusing function call with undefined
async function fetchData() {
setTimeout(() => {
return { data: [1, 2, 3] };
}, 1000);
}Solution
Step 1: Analyze return behavior
The function does not return anything; setTimeout callback's return is ignored.Step 2: Understand async function expectations
Async functions should return a Promise; here, no Promise is returned, so caller gets undefined.Final Answer:
The function does not return a Promise, so data is undefined -> Option BQuick Check:
Async function must return Promise, not just use setTimeout [OK]
- Assuming setTimeout return affects function return
- Thinking async forbids setTimeout
- Ignoring that async functions return Promises
Solution
Step 1: Identify filtering and delay
async function fetchUsers() { const users = [{ name: "", id: 1 }, { name: "Eve", id: 2 }]; return new Promise(resolve => setTimeout(() => resolve(users.filter(u => u.name)), 1000)); } filters out users with empty names and returns a Promise resolving after 1 second.Step 2: Check other options
A returns unfiltered data with delay, B performs filtering but is synchronous and non-async without delay, C returns filtered data synchronously without delay.Final Answer:
async function fetchUsers() { const users = [{ name: "", id: 1 }, { name: "Eve", id: 2 }]; return new Promise(resolve => setTimeout(() => resolve(users.filter(u => u.name)), 1000)); } -> Option AQuick Check:
Filter then delay with Promise = async function fetchUsers() { const users = [{ name: "", id: 1 }, { name: "Eve", id: 2 }]; return new Promise(resolve => setTimeout(() => resolve(users.filter(u => u.name)), 1000)); } [OK]
- Not filtering data before resolving
- Returning data synchronously without delay
- Using non-async functions for async behavior
Ready to prove it?
Pass the test and this pattern moves unknown
