Asynchronous JavaScript Tutorial #2 - HTTP Requests

Net Ninja · Beginner ·🌐 Frontend Engineering ·5y ago

Key Takeaways

Makes HTTP requests using the Fetch API in asynchronous JavaScript

Full Transcript

okay then so now we know how asynchronous code generally works and how we can use callback functions which can run code after an asynchronous task is complete let's now turn our attention to http requests which are the things that are ultimately going to be the actions which take some time to complete in your code so first of all what is an http request and why would you want to make one well sometimes we want to show stuff on our website which is stored in some kind of database or in another server somewhere such as blog posts or comments or maybe a list of songs or even user data for a profile page now all of that data could be stored on another server somewhere or in your own database so we'd make what's known as an http request to reach out to that external server on that database to get that data and then do something with it now when we make those requests we can make them to what's known as an api endpoint and these are just like urls that a particular api or server exposes to us so that we can use them to get data from them so for example imagine we had a song library api like spotify or soundcloud it might have an endpoint which looks something like this so making a request to this endpoint would return us a list of moby songs so from our code which runs in the browser we would make a request to a server endpoint now that server would look at this request and say okay you want this data so i'll send it back to you as a response and then we get that data in our code and we can do something with it like render it to the browser if we want to now there's many different apis that we can use to get data twitter youtube instagram spotify and loads more and each api is going to have its own set of endpoints that we make requests to for data you could even make your own api using a server-side language such as python ruby or even javascript if you're using node.js but anyway once we make a request to an endpoint from the browser we typically get back a selection of data in a format called json json is a format which looks very much like javascript objects and then we can do something with that data so the api that we're going to be using to practice with in this chapter is the json placeholder api and i'll leave this link attached to the lecture so this is a nice completely free service that allows us to play around with api endpoints and to get back some json data now we can see an example of this in action if we scroll down right here this is an end point and if i click try it then this website is going to make a request to this endpoint and show me the data down here so i'll click that and we can see this data right here so this looks very much like json data this is the kind of data we would get back from a server now likewise i could take this endpoint and i could just place it up here and paste it and press enter and that is going to get me the same thing we're still making a request it's just that this time we're directly making the request inside the browser to go and get that data now if i open up the dev tools and then i go to the network tab over here we can see if i refresh this we're going to see this thing right here this is the network request and we can see some different information about the network request right here we can see the request method is a get method and when we want to get data from somewhere typically that's what we do we make a get request we can see the request url right here which is what we added to the browser at the top and if we go to the response tab over here we can see the data that we get back and this is json data so our task will be to interact with these api endpoints like this and make http requests to them from our javascript code so that we can receive a response and get some data back like this and then do something with it like output in a dynamic html template in the browser so then we know what an http request is now and why we'd use it to get some external data via some kind of api endpoint so now let's see how to actually make a request from our javascript code so the first thing we need to do is make a request object so i'll create a constant and i'm going to call this request and the way we create a request object is by saying new and then xml http request so this creates us a request object now this xml part represents an older data format we used to work with much before jason arrived on the scene but this request object can work with any kind of data now xml json plain text etc so now we have this request object and this is the thing that is going to be used to actually send a request from the browser this thing is built into the javascript language so now we have this request object we can actually use it to send a request to get some data so how do we do that first of all we take the request variable we just created and now this comes loaded with different properties and methods we can use the first method we need to use is the open method now this open method takes two arguments the first argument is a string and it's the type of request we want to make now remember when we get some data we make a get request now there are other types of requests that we can make like post to send data or put to update etc there's loads of different types but we want to get some data and for that we make a get request so that is the first argument right there the type of request we're making now the second argument is where we want to make the request to what is the end point we want to get data from so if we go to the json placeholder and scroll down a little bit the endpoint we want is right here this is the data we want now copy all this except for the one the one at the end gets us a single to do but we want all of the to do's and this url right here will do that without the one it will get us a list of to do's so copy that and head back to the code and we need this inside a string again so paste that inside there now so now we're telling our request what the type of request is and where to get that data from where to send the request to but at the minute the request isn't actually being made this is just kind of setting up the request so to actually send the request we need to say request dot send and that sends the request so if i save this now and head to the browser i'm going to go to the network panel over here and then i'm going to just refresh and we can see now if we go over here we can see this request made here so if we click on that request we can see it's a request method of get this is the end point and the response over here is all of this data but at the minute we don't know in our code when this is complete we don't know how to access that data so how do we do all that well in our code we can track the progress of a request using an event listener and a specific event called ready state change so we attach this to the request object itself so we can say request dot add event listener and the event is called ready state change all one word all lowercase so that's the event and this fires every time there's a state change in the request now a state change just means that the request is going through these different phases of the request and there's four in total so what i'm going to do right here is just log those out to the console first of all i'm going to say console.log and then the request object every time there's a state change and then i'm going to log out request dots ready states and that gets us the state that the current request is in so if i save this what's going to happen is we're going to set up the request then we're going to send it and every time there's some kind of ready state change then it's going to log these to the console so let me save that and come over here and come to the console and you can see there's four in total like i said so each time around we get the request object which has all of these different things on it and up here we can see we also have the states one two three and four but what do these actually mean well to find out we're gonna head on over to the mdn guide the mozilla website and we can see down here these different ready states so we have zero which means it's not yet been sent and open has not been called remember we use open right here then we get ready state one when we've actually called open so after this point right here we're gonna get ready state one then after we use the send method we're gonna get ready state two which is this point then ready state three is when we're downloading and we've got some kind of response text and then four is when the operation is fully complete and we've got access now to the response so this right here this number four this is the most crucial step because at this point this is when we want to take that data and do something with it we can receive it and do something so if we head back to these different things right here if we open up this object and come down here we can see this response text and this response text contains the actual data that we receive so over here what i'm going to do is listen inside this event listener for the ready state changes and if the ready state is equal to four that's the point when we can do something with the response text so what i'm gonna do is comment this thing out first of all and then i'm gonna do a little if check and inside here i'll say if request dot ready state is triple equals to four then we're going to do something this is when we can do something with the response the data not in zero one two or three we can't do it at those stages because it's not ready yet but we can do it at this stage so we only want to do something at stage four and that's why we're doing this if check because then whatever we do with it we can do that here so for now all i'm going to do is console and say request dot response text that's the property which contains the response data so if i save this now then come to the browser we're going to see all of this data returned to us so we have all that now that's really nice and it looks like a gigantic array of javascript objects but this is actually json data it's all really a giant string called jason and we'll look at how we can deal with this data later on but for now just know this is the data that we're getting back so now we're sending the request and we're reacting when it's complete and we get a response and we can see that data in the console so we're two thirds of the way there but this isn't complete yet to complete it fully i want to talk about response statuses and we'll talk about that in the next video

Original Description

Hey gang, in this Async JavaScript tutorial we'll take a look at what HTTP requests are and how they are made under the hood. Later we'll use the Fetch API to make these requests. Get the full Modern JavaScript tutorial on Udemy here (discount auto-applied): https://www.thenetninja.co.uk/udemy/modern-javascript 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join ---------------------------------------- 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Helpful Links: + Get VS Code - https://code.visualstudio.com/ 🐱‍💻 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Prepare for a frontend developer interview with Capgemini by reviewing JavaScript fundamentals and practicing common interview questions
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with 10 essential tools for modern web app development
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with top 10 developer tools in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
US frontend engineer hiring demand peaked in 2022 and remains flat-depressed in 2026, contrary to common assumptions
Dev.to AI
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →