Fetch API

The fetch api is a powerful tool used by the JavaScript interface to access and manipulating different parts of the HTTP pipeline. It also allows for fetching resources across the network with a global fetch() method. Before fetch there was XMLHttpRequest, the fetch api provides an alternative that can be easily used. Using fetch is essential for the functionality of JavaScript.

The fetch method requires a URL as a parameter used as a resource to fetch. This action returns a promise that you can use then() and catch() methods to handle it. At this point the promise becomes a response object. The response object is filled with many useful properties which can be used to inspect the response.

If there is a network error it usually means theres a permissions issues causing the promise to be rejected. This type of error happens when a user is offline or DNS lookup failure. One error message that i have seen many time is “Errr: internet server error” , This lets me know whether or not my HTTP response’s response is in range. Error messages is a great place to begin trouble shooting your code.

When using JavaScript and Rails together, fetch() is to communicate the frontend with the backend. Essentially the frontend mirrors the backend displaying key information and functionality. This concept at first was hard to grasp but once i understood the concept my abilities as a coder greatly expanded. In order to access a response you first have to know where this response should come from.