From the course: JavaScript: Ajax and Fetch

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Handle basic fetch errors

Handle basic fetch errors - JavaScript Tutorial

From the course: JavaScript: Ajax and Fetch

Start my 1-month free trial

Handle basic fetch errors

- [Instructor] Each method in a fetch chain finishes by either resolving or rejecting. Resolving means the process executed successfully while rejecting means there was an issue and it throws an error. You might expect that receiving an HTTP response like 404 would result in a fetch request rejecting. However, fetch rejects only when something like a network issue prevents the request from being executed. However, the response object returned by the fetch method includes a Boolean OK property that is true only if the HTTP response is in the 200 range. Otherwise, this property is set to false even though the fetch itself doesn't throw an error. It's a common pattern when using fetch to check the OK value first before attempting to work with the data. This way, you can intercept any errors and handle them differently than you would a successful response. Back in my code, I could add error checking to the first then method. But anything more than a return statement I prefer to move into…

Contents