Interview Question

What is fetch API?

fetch resolves with a Response; check response.ok before reading successful data.

💡 Concept ✅ Quick Revision ⚡ JavaScript

Answer

The Fetch API exposes request and response objects and a promise-based fetch operation. • fetch returns a promise that fulfills with a Response when a response is available. • HTTP error statuses such as 404 do not automatically reject the fetch promise. • Response body methods such as json also return promises.

Example

Code
fetch('/api/items')
  .then(response => {
    if (!response.ok) throw new Error(`HTTP ${response.status}`);
    return response.json();
  })
  .then(console.log);

Quick Revision

fetch resolves with a Response; check response.ok before reading successful data.