- Data Types
- Conditional Statements
- Switch Case
“If you just communicate, you can get by. But if you communicate skillfully, you can work miracles.” — Jim Rohn
So…what exactly is fetch?
The reason I invoked a fetch function in my code was to persist the data on the client side(the input value of a user’s comment form) to an external resource to which I can communicate with for saved data in my database(Rails API). For example, if I needed to return all comments, I can make a fetch request to retrieve all comments. If I wanted to delete a comment from my data base, I can make a fetch request to the database from the client-side to select and delete the comment without refreshing or changing the webpage.
The Fetch API has following interfaces from the example:
- fetch(): The fetch() method used to fetch a resource on line 65.
- Headers: Represents response/request headers, allowing you to query them and take different actions depending on the results. In the code above, the headers is an object that defines the type of data that is being passed through.
- Request: Represents a resource request. The fetch() function takes one mandatory argument, the path to the resource you want to fetch in this case it was a call to the Rails API URL where the data would be displayed. It returns a promise (a proxy for a value not necessarily known when the promise is created.), whether it is successful or not.
What else can fetch do?
Fetch can be used in conjunction with CRUD capabilities when asynchronously handling data from the client-side to the database. It uses different types of methods whose value is the HTTP action. The default method is the GET method; however, other methods include: POST, PATCH, and DELETE.
This is an example of a basic class method that utilizes a fetch function to the rails api to return all commments. The default fetch method is GET which is why it is not explicitly defined!
This is an example of an instance method that utilizes a fetch function to the api with a RESTful route to the API that specifies the comment that is slated for destruction. As you can see, the fetch function is relatively simpler because it is executing the client-side command to delete which simsply destroys the data from the database!
Now go get that ball! *wink*