November 2018
Beginner
502 pages
10h 22m
English
As we would expect with a promised-based function, we handle errors in the catch method:
fetch("https://jsonplaceholder.typicode.com/posts") .then(response => response.json()) .then(data => console.log(data)) .catch(json => console.log("error", json));
However, the catch method doesn't catch responses that aren't in the 200 range. An example of this was in the previous example, where we got 404 in the response status code. So, an HTTP error status code can be handled in the first then method and not the catch method.
So, what is the catch method for? The answer is to catch network errors.
So, that's how to get data using fetch. In the next section, we'll cover posting data.
Read now
Unlock full access