Asynchronous JavaScript Tutorial #3 - Status Codes
Skills:
JavaScript Fundamentals90%
Key Takeaways
Explains status codes in asynchronous JavaScript programming
Full Transcript
so right now we're checking this thing over here request dot ready state is equal to four and at that point the request is complete and then we're logging the response text to the console and that's fine but this check in itself to say okay well the request is complete that's not enough because if there was some kind of error with the request for example if i create some kind of end point which is not valid it still tries to make this request and it still goes through the motions of going through each phase or state and it will reach state four but the difference is we're not going to get any kind of data back because this is not correct there will be some kind of error along the line and although it will reach stage four to say yet the request is now complete it will come back with some kind of error code to say there was a problem along the line somewhere and obviously we won't have access to the response so let me just demo this i'm gonna just log out the request object in itself here as well as the response text and i've added this extra s to the url the end point so obviously this doesn't exist and we should see some kind of error but i'm going to log out the request anyway and try to log out the response text so let me save this and come over here and now we can see first of all we get this 404 error and we get this thing right here this is the request object notice we don't see any response text when we're logging this we don't see over here because we're not getting that data anymore because there's an error now if we open this up and we come down here we can see the status right here is 404 and the response text is empty so we're still reaching ready state 4 and we're still firing this code that's not a problem we're still getting there it's just that there's been an error along the way and now we're getting this bad status 404 and also no response text so what we need to do to overcome this is as well as checking the ready state also check the status when the request comes back so if i correct this again to the correct endpoint and save it notice now we get the data and also up here we can see this object if we expand that we get a status of 200. we're at readystate4 we get the response text and we have a status of 200. now a status of 200 means that everything was okay with the request and it's come back with the data essentially now the 404 request that we just saw means it cannot find the resource that we're trying to send it to and it can't find it because it didn't exist when we added this s on that resource this endpoint it didn't exist so that's why we get a 404 error sometimes when you go to a web address up here that doesn't exist you'll probably see some kind of 404 error in your browser that's the same kind of error now there's loads of different types of status codes and you can get them all from a lot of different websites this one the mozilla mdn guide lists every single one as well so typically we get codes in the ranges of the 100s 200s 300 400s and 500s and in each range it means something a bit different so all of the status codes in the 100 range they are to do with information responses anything to do with success responses is in the 200 range we can see 200 right here is okay and then the 300 range is any kind of redirection messages the 400 range is client error responses so this is when there's an error in the browser such as when we formatted something incorrectly in the code or if we use a wrong endpoint etc this is the one we saw 404 not found and then 500 is when there's a server error over here so it's not our fault we've made the correct type of request but there's some kind of error on the server which prevents us from getting the data so they're the different kinds of response codes what we want to do is to make sure that we have this response status so as well as checking for ready state 4 over here we can also check for the status right here to be 200 so we've seen how to do logical and double ampersand and we say request dot status is equal to 200. now this is only gonna fire when we get an okay response and ready stay equal to four so let me try this again i'm gonna save it and notice now we don't log out this thing right here the request object because this now is not firing because the status is not 200 we still get this error up here to say we've got a 404 error but we're not trying to do something now over here we're only doing this when we have a correct endpoint so if we save it now we can see all of this stuff here and the object so what we could do is attack on an else clause and in fact we'll say else if and then in this if statement we're still going to check this thing over here like so now if this is the case if the ready state is still equal to 4 but obviously at this point the status is not 200 so something's maybe not okay then we can fire this code block over here so the request is finished but there's been some kind of problem along the way and we probably don't have any kind of response text so what we could do here is say console.log could not fetch the data okay so if we save that now and try something like adding an s on save it and we can see now we get this little error message in the console could not fetch the data so now we have our http request set up the next step is to write a callback function which is going to execute when this task is complete and we'll tie this back into our understanding of asynchronous code
Original Description
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: JavaScript Fundamentals
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI