For this unit, we will be watching these Asynchronous JavaScript videos.
Read Chapter 8 (pages 367 - 377, 384 - 408)
1. Static content and user interactions!
2. Dynamic Content and More Data
A click from the user changes the UI,
but the data is the same of all users.
Adding Data (often user data)!
New unique data is added specifically for the user who is currently using the app.
In databases across the Internet!
Backend languages handle the requests for data about users. Databases hold all of the information about our users.
What happens if that language is different from JavaScript? And how do we make those requests?
JSON = JavaScript Object Notation
{
"firstName": "Gene",
"lastName": "Smith",
"address": {
"streetAddress": "425 2nd Street",
"city": "San Francisco",
"state": "CA",
"postalCode": 94107
},
"phoneNumbers": [
"212 732-1234",
"646 123-4567" ]
}
Your product manager provided you the following UI design. Use JSON to store the data.
GET and POST Requests
Add the following URL into your URL bar in your browser.
Your browser will automatically make a GET request, and this particular response will be JSON. This is very common for when you are making API calls to get data.
Click here for an example.
The GET request on the prior slide yields JSON in raw form.
To make the JSON more readable, we will need to parse it.
Click here to add JSON Formatter,
an extension from the Chrome Web Store.
Then click between Raw and Parsed. Notice what happens!
Web apps can be set up to make many, many GET and POST requests for almost any user action.
Frontend developers send those requests and receive the responses.
Frontend developers also determine WHEN a request is sent and how it is handled by the UI.
Asynchronous technologies enable users to access information or communicate at different points of time, usually at the time of choice of the user.
A traditional data-driven web application is written so that it:
This process has a negative effect....
As a result, the user has "down time" - a period of time where the users can't interact with the webpage at all.
As you load more listings on Airbnb, you are sending requests for more data.
Technologies Used: | Technologies Used: |
|
|
There are different methods for making async requests, and new ones are being added all the time.
We will talk about a tool that is built into JavaScript, called Fetch.
The Fetch API allows JS to access and manipulate the HTTP pipeline, such as requests and responses, using fetch()
.
Let's take a look at "GET
" and "POST
"
//GET
fetch('http://example.com/movies.json')
.then(response => response.json())
.then(data => console.log(data));
//POST
var data = { name: "lhack"}
fetch('http://example.com/movies.json', {
method: 'POST',
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => console.log(data));
//GET
fetch('http://example.com/movies.json')
//POST
var data = { name: "lhack"}
fetch('http://example.com/movies.json', {
method: 'POST',
body: JSON.stringify(data)
})
Notice that both requests have a url string as a parameter.
The POST request has an additional parameter: an object.
The object in the POST request has 2 properties:
method
, which must have a value of 'POST'body
, which must utilize the JSON.stringify() method.
When using fetch()
, expect a response
and data
.
Let's look at "GET
" and "POST
" again.
//GET
fetch('http://example.com/movies.json')
.then(response => response.json())
.then(data => console.log(data));
//POST
var data = { name: "lhack"}
fetch('http://example.com/movies.json', {
method: 'POST',
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => console.log(data));
Notice we can use method chaining and apply .then()
right after we use the fetch()
method.
Let's walk through this example
We will check out this example using the Cat Facts API
Click here
$('.btn').on("click",function() {
$('.text').text('loading . . .');
fetch('https://cat-fact.herokuapp.com/facts')
.then(response => response.json())
.then(data => {
for (let i = 0; i < data.all.length; i++) {
$(".text").append("" + data.all[i].text + "
")
}
});
});
Hint: Take a look at the structure of the JSON data to complete the exercise.
Instead of .then()
function getLyrics() {
fetch('https://api.lyrics.ovh/v1/coldplay/42')
.then(response => response.json())
.then(data => console.log(data));
});
}
Async/Await
//async/await refactor
async function getLyrics() {
let response = await fetch('https://api.lyrics.ovh/v1/coldplay/42');
let lyrics = await response.json();
console.log(lyrics);
}
.then()
.await
allows you to write these data calls in order.Learn how to implement async/await. Read here.
We have used fetch()
to get data using the Cat Facts API.
Click here to refactor using async
/await
.
Ask a user for a number, then make an API call to fetch and then display a trivia fact about that number to the user.
Click here for the instructions and links to resources to help you use async
/await
.
Watch these Asynchronous JavaScript videos.
Thank you for your attention!