10 - React API.
Speck Academy 2021
API
AJAX
Fetch API
Async/Await
1
2
3
4
Overview
1
API
API
API - Application Programming Interface
Code that allows two software programs to communicate with each other.
Contract between a provider of some service and a client of those services which defines:
API
In most cases you’ll use APIs that are created in the way that REST architectural style defines.
REST (Representational State Transfer) is an architectural style for providing standards between computer systems on the web, making it easier for systems to communicate with each other.
REST
Separation of the client and server
Statelessness
REST
In the REST architecture, clients send requests to retrieve or modify resources, and servers send responses to these requests.
Request consists of:
REST
Response consists of:
REST
Let’s make some requests to the API.
In order to do that we can use Postman or Insomnia client (download Postman and try to make some requests to the routes specified inside Hands-on).
Together we will test requests that we will later implement in our React demo application.
You can find a lot of useful information about the REST architecture here.
2
AJAX
AJAX
AJAX stands for Asynchronous JavaScript And XML.
It is the use of the XMLHttpRequest object to communicate with servers.
It can send and receive information in various formats, including JSON, XML, HTML, and text files.
Asynchronous nature means it can communicate with the server, exchange data, and update the page without having to refresh the page.
It is well supported in all of the major browsers, even in old ones, such as Internet Explorer (check browser support for XMLHttpRequest API here).
AJAX
Instantiate XMLHttpRequest object and define how to process response from server inside onreadystatechange function.
First parameter of the open() function is the HTTP request method (GET, POST...) and second is the URL you’re sending the request to.
Any data you’re POSTing to server should be placed inside send() function.
AJAX
Inside AJAX request we can also have:
AJAX is quite a old implementation, but still offers us a lot of features. If you need to use traditional XHR then check jQuery.ajax() to make things easier.
Most popular modern alternatives to traditional XHR are Fetch API and Axios.
3
Fetch API
Fetch API
Fetch API provides an interface for fetching resources and it is a modern alternative to XMLHttpRequest.
Available in multiple interfaces (specifically Window and WorkerGlobalScope).
fetch() method returns a promise that resolves to the response of that request whether it was successful or not.
Fetch is clean, elegant, simpler to understand, but it is not a full drop-in replacement for traditional AJAX.
Fetch is quite a new in development, so please check browser support before use.
Fetch API
On the image on the right you can see example of GET request on the top of code.
The json() is a method of the body mixin that returns a promise that resolves with the result of parsing the body text as JSON.
Because json() returns a promise we can chain another then() which logs the parsed response
Fetch API
Uncommented code shows an example of POST request used for user registration.
Fetch API
fetch() allows us to make other types of requests as well like POST, PUT, DELETE and so on.
We can provide the headers (most often Content-Type or the Authorization), body of a request in case of POST and PUT requests.
If we compare Fetch and XHR:
4
Async/Await
Async/await
async and await are extensions of promises.
Asynchronous function operates asynchronously via event loop.
async use an implicit Promise to return its result. Even if you don’t return a promise explicitly, async function makes sure that your code is passed through a promise.
async function can contain an await expression that pauses the execution of the async function and waits for the passed Promise’s resolution, and then resumes the async function and returns the resolved value.
await only can be used inside async function.
Async/await
Example explanation:
Async/Await vs Promises
These two concepts are similar, but each of it has its own purpose.
Useful resources (please read):
5
Hands-on
Hands-on
6
Homework
Homework