Asynchronous JavaScript Tutorial #1 - What is Async JavaScript?

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

Key Takeaways

Explains the basics of asynchronous JavaScript

Full Transcript

hey there gang and welcome to this asynchronous javascript tutorial now i do already have an asynchronous js course on youtube but that's now about four years old so i'm putting up a more up-to-date series which is straight from my udemy modern javascript course and it's going to cover things like the fetch api promises async and weight as well as all of the basics and building blocks of what asynchronous code is so i hope you enjoy it and if you want to take the full udemy course the link is going to be down below in the description [Music] asynchronous javascript is one of the most important parts of the language because it governs how we perform tasks that take some time to do like requesting data from a database or from an api and chances are if you're making a real javascript application then you will be using asynchronous code at some point to do these kinds of things so in a very simplistic nutshell asynchronous code is code that can start now and then finish later but before we talk more about asynchronous code let's first of all understand its counterpart synchronous code so javascript by its very nature is a synchronous language and that basically means that javascript can execute only one statement at a time from top to bottom so for example in a javascript file we could have three statements and each statement is run in turn now line two cannot start before line one is finished and line three cannot start before line two is finished so it executes these one at a time in order now you might hear javascript being called a single threaded language and that essentially means the same thing a thread is like an ordered sequence of statements and only one of those statements can run at a time so this is the crux of synchronous code one statement being executed at a time after one another now with this image of synchronous code in mind a single thread and only one statement executing at a time imagine this scenario we want to call a bunch of javascript functions in a file some of these functions do quick little things like log something to a console but one of them wants to make a network request to a database on another server somewhere to get some data now this could take two or three seconds to complete maybe less maybe more so in a synchronous programming world because only one statement can run at a time this fetching of data stalls the program this is known as blocking code because it blocks the next line of code from running until it gets the data back and this function is complete after those two or three seconds now in this case you might think well okay two seconds isn't that long to wait but what if you have multiple functions to get data then that could soon be five to six seconds or six to seven seconds and it would block the rest of the code underneath from running until these things are complete so this is a downfall of synchronous code and this is where asynchronous code comes into play to help us out so we know that running our functions synchronously when it comes to task that takes some time to complete is probably then not the best way to work right so remember the definition i first gave you of asynchronous code to start something now and finish it later this is the pattern we generally want to follow when running tasks that take some time to do like network requests for data to a database or an api so imagine we have the same kind of sequence of function calls or statements only this time instead of this being some kind of synchronous function to request data we use an asynchronous function instead and this means the function can start now and then finish later once the data has come back from wherever we get it from now since this function is finishing later what we typically do is pass this function or this statement some kind of callback function as a parameter and then that callback function is the thing that runs and finishes later on once the request is complete and the data comes back so how is this all working exactly well we have our queue of function calls in the code again which executed one at a time so first this one then this one and so forth now remember at all times javascript can only execute one thing at a time but this time when we get to the request function here we are using an asynchronous function to request our external data what this means is that the browser takes that request and it handles it outside of the scope of this single thread in another part of the browser it also takes a callback function and puts it to one side too so that it knows to execute this later on when the data comes back so because this network request has been taken out of this thread and is now running in a different part of the browser javascript can carry on down the queue and run the remaining functions all the while this is still going on the request for data so it continues through these functions and then when it receives the data back from the network request and once the rest of the functions have been executed then we're allowed to call this callback function and finish this original function so this is the crux of asynchronous programming starting something now which can be finished later and it makes our code non-blocking because the rest of the functions here they can run while the request is being made now this explanation is a very simplistic one and there are other things at play such as the event loop and the call stack which we've not discussed but i think to delve into that right now would be a bit overwhelming and i think this picture of painted should be enough for now to understand the general idea of asynchronous code so now we know a little about what that is and what asynchronous code does let's have a look at an example okay then so hopefully now you understand at least the basic principle of asynchronous code and how javascript has a single thread and runs through one function at a time or one statement at a time so i want to do a quick demo of this in action a very quick simple one so the first thing i'm gonna do is paste in four console logs so if i save this we know now that javascript is gonna execute these one at a time from top to bottom and in the console we get one two three four as expected now what i'm gonna do in the middle is i'm gonna do a set timeout and we've seen this before it's where we're passing a function and that function fires after a certain amount of time that we specify so let's do the function first of all and inside here we'll console.log and we'll say the callback function fired okay and we want to fire this after well let's just say 2 000 milliseconds so that's two seconds so this right here this is meant to emulate some kind of network request that takes time to do so imagine this is going out to get data and it takes two seconds to do then when it comes back it's gonna fire a callback function it's the same kind of thing in this case we're not getting data we're just waiting two seconds to emulate that request and then we're firing this so this right here this is a synchronous code in action it's gonna start when the file runs it's gonna wait two seconds and then it's gonna finish later by firing this callback function now is this gonna block the rest of the code will it go one two then wait two seconds and fire this then 3 4 or will it not block the code well i just said that this is asynchronous so it doesn't block the code and i'll show you that if i save it we see one two three four then after two seconds we get this so if you imagine that image of a thread i showed you before and these things are on it it fires this then this then this and then it waits two seconds over here and passes the callback function somewhere else and then it fires this then this and after two seconds the callback function is added to the bottom over here and this is where it fires at the end after one two three four so this does not block the code so in the future instead of using set timeout we'll be using real http network requests for data but this kind of sets the scene of how it all works it will be the same principle so now we know the general principle behind it of asynchronous code i'm going to show you how to actually make network requests

Original Description

Hey gang, in this JavaScript tutorial series we'll dive into async js - from the very beginning. Rather than just start using fetch, async & await from the start, we'll take a closer look at async code & how it all works under the hood. 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 →