React / Next.js

Data Fetching & API

fetch API, axios, async/await, error handling, loading states, abort controllers

20 interview questionsยท
Mid-Level
1

What does the fetch() method return when called?

Answer

fetch() returns a Promise that resolves with a Response object. Unlike axios, fetch does not automatically reject HTTP errors (like 404 or 500), so it's necessary to check response.ok before parsing the data. This approach gives more control over error handling but requires explicit status checking.

2

How to extract JSON data from a Response object obtained with fetch()?

Answer

The Response object has a json() method that returns a Promise resolving with the parsed data. This method is asynchronous because it reads the response body progressively. It's important to first check response.ok before calling json() to ensure the request succeeded.

3

Which property of the Response object allows checking if the HTTP request succeeded?

Answer

The response.ok property returns true if the HTTP status code is between 200 and 299, indicating a successful response. This property is essential because fetch() doesn't automatically reject HTTP errors, unlike axios. Checking response.ok before processing data is a best practice to avoid parsing error responses.

4

Which keyword allows waiting for a Promise to resolve synchronously in an async function?

5

In what order should the following methods be called during a fetch(): json(), then(), catch()?

+17 interview questions

Master React / Next.js for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free