Asynchronous Vs Synchronous Programming

Web Dev Simplified · Beginner ·🌐 Frontend Engineering ·7y ago

Key Takeaways

The video explains the concepts of asynchronous and synchronous programming in JavaScript, using tools like Visual Studio Code, Node.js, and JavaScript functions such as setTimeout and fetch.

Full Transcript

hello everybody kyle here from webdev simplified in this video i'm going to be going over a synchronous versus synchronous programming which is a concept that confuses many beginner to intermediate level developers it's also a pattern that is incredibly common in JavaScript and especially in nodejs so something that you need to have a fairly strong understanding of before you can start to truly use these languages and frameworks to their full potential so let's get started now I'm going to start by explaining the differences between asynchronous and synchronous code synchronous code will start at the very top of a file and execute all the way down to the bottom of file each line in order until it gets to the bottom and it will stop a circulars code starts out very similar we'll start at the top of the file and execute the code until it gets to the bottom but during that execution it'll run into certain asynchronous functions or code or it will split off and execute that asynchronous code separately from the rest of the code and that's usually because it needs to wait reduce some operation that takes a long period of time so the synchronous code will execute from the top to the bottom but the ACE winners code will start at the top and execute until it hits something that is asynchronous and then it will execute that and the rest of the code at the exact same time and it will do that for every ace your goodness thing it hits so you may have multiple different threads running your different code in different sections so your code may execute in a different order but in synchronous code it always executes in the exact same order and that's really where the difference comes from asynchronous code is harder to work with because it'll execute in a different order every single time potentially which means that you have to make sure that your code will work no matter which path it takes whether it executes everything in order and reverse order or any other scrambled order that you haven't thought about before so let's jump into some examples in Visual Studio code showing you the differences between asynchronous and synchronous code right now I just have a really simple JavaScript file open and on the right I have the console for that file open so we can see the different things that I print out I'm going to start by writing an incredibly simple synchronous file here so we'll just start by declaring two variables we'll say a equals 1 and B equals 2 and then we're going to log out those variables to the screen so we'll log a and then we're going to log B and if we say that you'll see it prints 1 and then 2 and that's because as we expect the first variable that we print here is a which equals 1 and the second variable we print is B which equals 2 so as we can expect there in order from top to bottom 1 and 2 and if we put anything else in here let's say we just want our console console dot log and if we just want to say synchronous and if we save that you'll see the dope M synchronous before 1 & 2 and as because it comes before and in synchronous files it will always execute top to bottom but what happens if we throw in some asynchronous code so let's do it here above all the rest of our synchronous code we're just going to use the set timeout function which is by nature and asynchronous function this function takes another function that it'll execute after a certain amount of time which you specify so we'll just tell this function here to do a log and we'll just say async code right there and then we have to specify how long we want it to take so we'll say it will take 100 milliseconds before it executes and if we save that you'll see that our async function down here prints after a synchronous and 1 2 3 even though it comes before them in the actual flow of the file itself and that's because this set timeout doesn't actually run the function in here until after these 100 milliseconds and at that point all of the rest of this code is already run because it sees this function queues this other function to log this after 100 milliseconds and then just keeps going as soon as it alongside and it doesn't wait for anything else to finish executing and that's what makes asynchronous code so powerful because you don't have to wait these 100 milliseconds in order to print out this log you just keep going with the rest of the code and then after hundred milliseconds it'll just come back to this function and set timeout is not the only form of asynchronous code in JavaScript that's built in the concept of promises which is something that you see whenever you see a dot Ben or dot catch coming after a function especially when you're doing fetching for example is another great example of asynchronous code that you don't actually know how long it's going to take since you don't specify the timeout so let's create a fetch function in here and we're just going to fetch the index page that I have which is just an empty page essentially and then so with promises you put that dot then and then you give it a function and this function is going to execute as soon as this fetch is done so it's going to fetch the index page of my application and then as soon as that's done it'll call this dot been function and inside of here we're just gonna log fetch and if we save that you'll see that fetch happens after synchronous a and B are printed out but it happens before async is actually printed out and that's just because fetch is quicker than 100 milliseconds in this case and it may look really simple you may think well of course that timeout will happen after the stuff that comes after it and fetch will happen after because it has to go get something but a lot of times what tricks people up is they may in this set timeout want to print out the a variable for example but afterwards they just say that a equals 10 for example so instead of 1 they think it'll print 1 here in this timeout and that it'll print 10 down here but in reality it's going to print 10 inside the timeout we can just say timeout here so we know which ones which and if we save this and run it you'll see that it's prints out 10 for a even though a is not set till 10 until after the set timeout but that's because the timeout as we said occurs a hundred milliseconds after it hits the line where it sets the timeout and we set a equal to 10 after the timeout and this is something that really confuses a lot of people especially one it's not obvious where your timeouts are and your async functions are and your variables start to get messed up this is why if you're using some form of asynchronous function it's almost always better to pass the variables into the asynchronous function other than relying on them from outside the syncher asynchronous function since they could be changed by the rest of your program without you actually knowing it and it could cause a lot of problems and a way to really spot asynchronous functions is every single asynchronous function for the most part is going to take a function as a parameter which is going to be called after a certain delay which is the a circuit as part of it not every single function that takes a function as an argument is asynchronous but in general most functions that take function arguments are going to be asynchronous so that's one way that you can spot these asynchronous style functions and that's really all there is to a synchronous versus synchronous code it's really a pretty straightforward concept but it can really be difficult to wrap your head around because it's not intuitive to have code executing in different threads and at different times than the way it's listed inside of the actual file here so hopefully this helps you understand the differences between asynchronous and synchronous code a bit better and allows you to use some of these functions like fetch and set time up more effectively without running into any bugs if you did like this video please make sure to subscribe to my channel for more videos just like this also check out my videos linked over here what you're going to be four more JavaScript related content also let me know down in the comments below anything that's confusing you guys so that I can make videos on these topics to hopefully help you and others out with this problem thank you guys very much for watching and have a good day

Original Description

Asynchronous code can be incredibly confusing and frustrating for anyone learning programming. In this video I will explain what asynchronous and synchronous code talk about their differences through the use of multiple examples. Twitter: https://twitter.com/DevSimplified GitHub: https://github.com/WebDevSimplified CodePen: https://codepen.io/WebDevSimplified #JavaScript #WebDevelopment #Programming
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Web Dev Simplified · Web Dev Simplified · 42 of 60

1 Introduction to Web Development || Setup || Part 1
Introduction to Web Development || Setup || Part 1
Web Dev Simplified
2 Introduction to Web Development || Understanding the Web || Part 2
Introduction to Web Development || Understanding the Web || Part 2
Web Dev Simplified
3 Introduction to HTML || Your First Web Page || Part 1
Introduction to HTML || Your First Web Page || Part 1
Web Dev Simplified
4 Introduction to HTML || Basic HTML Elements || Part 2
Introduction to HTML || Basic HTML Elements || Part 2
Web Dev Simplified
5 Introduction to HTML || Advanced HTML Elements || Part 3
Introduction to HTML || Advanced HTML Elements || Part 3
Web Dev Simplified
6 Introduction to HTML || Links and Inputs || Part 4
Introduction to HTML || Links and Inputs || Part 4
Web Dev Simplified
7 Learn Git in 20 Minutes
Learn Git in 20 Minutes
Web Dev Simplified
8 5 Must Know Sites For Web Developers
5 Must Know Sites For Web Developers
Web Dev Simplified
9 10 Best Visual Studio Code Extensions
10 Best Visual Studio Code Extensions
Web Dev Simplified
10 Learn CSS in 20 Minutes
Learn CSS in 20 Minutes
Web Dev Simplified
11 How to Style a Modern Website (Part One)
How to Style a Modern Website (Part One)
Web Dev Simplified
12 How to Style a Modern Website (Part Two)
How to Style a Modern Website (Part Two)
Web Dev Simplified
13 3D Flip Button Tutorial
3D Flip Button Tutorial
Web Dev Simplified
14 How to Style a Modern Website (Part Three)
How to Style a Modern Website (Part Three)
Web Dev Simplified
15 Animated Loading Spinner Tutorial
Animated Loading Spinner Tutorial
Web Dev Simplified
16 How to Write the Perfect Developer Resume
How to Write the Perfect Developer Resume
Web Dev Simplified
17 Animated Text Reveal Tutorial
Animated Text Reveal Tutorial
Web Dev Simplified
18 Learn Flexbox in 15 Minutes
Learn Flexbox in 15 Minutes
Web Dev Simplified
19 Custom Checkbox Tutorial
Custom Checkbox Tutorial
Web Dev Simplified
20 Start Contributing to Open Source (Hacktoberfest)
Start Contributing to Open Source (Hacktoberfest)
Web Dev Simplified
21 JavaScript Shopping Cart Tutorial for Beginners
JavaScript Shopping Cart Tutorial for Beginners
Web Dev Simplified
22 Responsive Video Background Tutorial
Responsive Video Background Tutorial
Web Dev Simplified
23 1,000 Subscriber Giveaway
1,000 Subscriber Giveaway
Web Dev Simplified
24 How To Prevent The Most Common Cross Site Scripting Attack
How To Prevent The Most Common Cross Site Scripting Attack
Web Dev Simplified
25 Transparent Login Form Tutorial
Transparent Login Form Tutorial
Web Dev Simplified
26 The Forgotten CSS Position
The Forgotten CSS Position
Web Dev Simplified
27 How to Code a Card Matching Game
How to Code a Card Matching Game
Web Dev Simplified
28 10 Must Install Visual Studio Code Extensions
10 Must Install Visual Studio Code Extensions
Web Dev Simplified
29 Learn CSS Grid in 20 Minutes
Learn CSS Grid in 20 Minutes
Web Dev Simplified
30 Learn JSON in 10 Minutes
Learn JSON in 10 Minutes
Web Dev Simplified
31 10 Essential Keyboard Shortcuts For Programmers
10 Essential Keyboard Shortcuts For Programmers
Web Dev Simplified
32 What Is The Fastest Way To Load JavaScript
What Is The Fastest Way To Load JavaScript
Web Dev Simplified
33 Differences Between Var, Let, and Const
Differences Between Var, Let, and Const
Web Dev Simplified
34 How To Install MySQL (Server and Workbench)
How To Install MySQL (Server and Workbench)
Web Dev Simplified
35 Learn SQL In 60 Minutes
Learn SQL In 60 Minutes
Web Dev Simplified
36 How To Solve SQL Problems
How To Solve SQL Problems
Web Dev Simplified
37 What Are Design Patterns?
What Are Design Patterns?
Web Dev Simplified
38 Null Object Pattern - Design Patterns
Null Object Pattern - Design Patterns
Web Dev Simplified
39 Your First Node.js Web Server
Your First Node.js Web Server
Web Dev Simplified
40 How To Setup Payments With Node.js And Stripe
How To Setup Payments With Node.js And Stripe
Web Dev Simplified
41 How To Learn Any New Programming Skill Fast
How To Learn Any New Programming Skill Fast
Web Dev Simplified
Asynchronous Vs Synchronous Programming
Asynchronous Vs Synchronous Programming
Web Dev Simplified
43 JavaScript ES6 Arrow Functions Tutorial
JavaScript ES6 Arrow Functions Tutorial
Web Dev Simplified
44 Are You Too Old To Learn Programming?
Are You Too Old To Learn Programming?
Web Dev Simplified
45 JavaScript Cookies vs Local Storage vs Session Storage
JavaScript Cookies vs Local Storage vs Session Storage
Web Dev Simplified
46 JavaScript Promises In 10 Minutes
JavaScript Promises In 10 Minutes
Web Dev Simplified
47 Builder Pattern - Design Patterns
Builder Pattern - Design Patterns
Web Dev Simplified
48 JavaScript == VS ===
JavaScript == VS ===
Web Dev Simplified
49 JavaScript ES6 Modules
JavaScript ES6 Modules
Web Dev Simplified
50 8 Must Know JavaScript Array Methods
8 Must Know JavaScript Array Methods
Web Dev Simplified
51 CSS Variables Tutorial
CSS Variables Tutorial
Web Dev Simplified
52 JavaScript Async Await
JavaScript Async Await
Web Dev Simplified
53 How To Choose Your First Programming Language
How To Choose Your First Programming Language
Web Dev Simplified
54 Easiest Way To Work With Web Fonts
Easiest Way To Work With Web Fonts
Web Dev Simplified
55 Singleton Pattern - Design Patterns
Singleton Pattern - Design Patterns
Web Dev Simplified
56 Responsive Navbar Tutorial
Responsive Navbar Tutorial
Web Dev Simplified
57 CSS Progress Bar Tutorial
CSS Progress Bar Tutorial
Web Dev Simplified
58 Learn GraphQL In 40 Minutes
Learn GraphQL In 40 Minutes
Web Dev Simplified
59 What is an API?
What is an API?
Web Dev Simplified
60 Learn How To Build A Website In 1 Hour!
Learn How To Build A Website In 1 Hour!
Web Dev Simplified

This video teaches the fundamentals of asynchronous and synchronous programming in JavaScript, covering topics such as promises, async/await, and non-blocking I/O. It provides practical examples and code snippets to help beginners understand the concepts.

Key Takeaways
  1. Write a simple synchronous JavaScript file
  2. Log variables to the screen
  3. Throw in some asynchronous code using the set timeout function
  4. Specify how long the asynchronous function should take to execute
  5. Create a setTimeout function to execute a function after a specified delay
  6. Use the fetch function to create a promise that executes after a function is completed
  7. Pass variables into asynchronous functions to avoid confusion
💡 Asynchronous code can be confusing because it can execute in a different order every time, requiring code to work in multiple scenarios. Understanding the differences between synchronous and asynchronous code is crucial for effective programming.

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 →