DevX JS - W3C9 - Async - Await
DevX JS - W3C9 - Async - Await
Async/Await
Review
Review
● JSON format: ● APIs
○ Key:Value pairs only! ○ API Requests
○ Only double quotes! ○ API Keys
○ All keys have (double) quotes! ○ OpenWeather
○ No functions! ○ Building an app with API
○ Curly braces for objects.
○ Square brackets for arrays.
Async/Await
Async/Await
async and await are two new keywords in
JS. They help you write requests. getUsers();
Why Async?
.then(function (res) {
return res.json();
})
.then(function (loginData) {
Why did we bother to learn two ways to fetch("/getClasses/" + loginData.userId )
write requests and promises? .then(function (res) {
return res.json();
})
Async is good when you have several,
.then(function (class) {
dependent API calls. Eg: fetch ("/getStudents/" + class.classId )
.then(function (res) {
1. Getting your account info return res.json();
})
2. Getting a list of your classes
.then(function (students) {
3. Getting a list of your students in
// Now you have everything!
those classes. // Login, class, and students!
});
You can’t get your classes until the app });
});
knows who you are, and can’t get your
// This is not real code! Just an example.
students until you know classes.
fetch("/login")
Callback Hell
.then(function (res) {
return res.json();
})
.then(function (loginData) {
In the example on the right, we see a fetch("/getClasses/" + loginData.userId )
concept jokingly called “callback hell”. .then(function (res) {
return res.json();
})
An API call makes another API call, and
.then(function (class) {
you end up with multiple nested function fetch ("/getStudents/" + class.classId )
calls and responses. This gets messy, as .then(function (res) {
you can see on the right. return res.json();
})
.then(function (students) {
Async await lets you do them all in the
// Now you have everything!
same scope. This is easier. // Login, class, and students!
});
});
});
// This is not real code! Just an example.
Just Async/Await
With async/await, we can rewrite multiple callbacks into cleaner code.
For a single API call, either method is fine. There’s not much code, and the async feature doesn’t
offer much benefit.
If you are making multiple, non-dependent api calls (eg, getting the weather AND news- separate
topics), async/await is actually WORSE! It is faster to use JS to make two requests at the same
time, rather than waiting for the first to finish!
For NOW I recommend picking either one and getting comfortable with it. Later in your career
you will be more worried about fast load times. But for now we are just learning how to do stuff!
async/await
async/await
We are going to re-factor a request into
async/await: getUsersSync(10);
It helps streamline the API KEY generation process, and has a searchable list of all the APIs it has
available.
The best part is that it has lots of example code that you can copy/paste to try out!
RapidApi
RapidApi
1. Go to https://rapidapi.com/hub, and
sign up for the service.
2. Look on the website for free APIs!
(Like really free. Some require a credit
card. Let’s stick to really free.)
3. When you find that free api, pick a
request type on the left.
4. Find some sample code on the right.
Set it to “javascript - fetch”.
5. Share in the chat the javascript object
you get!
6. Explore the APIs, which are useful?
Share in the chat!
Q&A