window.fetch() returns a Promise that resolves to a Response object.
🔹 What exactly does fetch() return?
const result = fetch("/api/users");
console.log(result);
✅ Return value
Promise<Response>
So:
fetch()does NOT return data directly- It returns a Promise
- That Promise resolves to a Response object
🔹 What is the Response object?
The Response object contains:
- Status code (
status) - Status text (
statusText) - Headers
- Body (as a readable stream)
Example:
fetch("/api/users")
.then(response => {
console.log(response.status); // 200
console.log(response.ok); // true
return response.json(); // another Promise
})
.then(data => {
console.log(data); // actual JSON data
});
🔹 Important Points (Interview Traps)
1️⃣ fetch() does NOT reject on HTTP errors
fetch("/404")
.then(res => console.log(res.ok)); // false
✔ Network failure → reject
❌ 404 / 500 → still resolve
2️⃣ Reading body is async
response.json(); // returns Promise
response.text(); // returns Promise
🔹 Using fetch() with async/await
async function getData() {
const response = await fetch("/api/users");
const data = await response.json();
console.log(data);
}
🎯 Short Interview Answer
window.fetch()returns a Promise that resolves to aResponseobject.
The actual response data must be read separately using methods like.json()or.text(), which also return Promises.
⭐ One-line summary
fetch() → Promise → Response → Promise → actual data.