Let’s begin the course with what most people mean when they say Asynchronous JavaScript.
For most people, Asynchronous JavaScript means a series of three steps:
- Send a request to a server
- Wait for a response to come back
- Then do something with the response
Unfortunately, this is not the proper definition of Asynchronous JavaScript.
That’s because these three steps are a series of actions you perform when you send a request to a Rest API — it uses Asynchronous JavaScript, but it is NOT asynchronous JavaScript.
For some people, Asynchronous JavaScript happens when you use the async and await keywords.
Unfortunately, this is also not the proper definition of Asynchronous JavaScript — it’s simply an asynchronous function.
// Asynchronous functionasync function someFunction() { await doSomething()}Before you can become fluent in Asynchronous JavaScript and Rest APIs, you have to understand what is Asynchronous JavaScript and what it is not.
To understand what it is, we can begin by understanding what Synchronous JavaScript is.
Synchronous JavaScript
JavaScript is synchronous in nature. This simply means JavaScript runs code in a sequence without stopping.
For example, these three console.log statements will be executed in sequence. One will be logged first, followed by Two, then Three.
console.log('One')console.log('Two')console.log('Three')
Asynchronous is the opposite of synchronous
JavaScript can be asynchronous too.
It becomes asynchronous when you write code that executes only after a certain event occurs.
Examples of these events can include:
- When a user clicks on a button
- When a user presses the keyboard
- When you have to wait for a response to come back from an API
- When you defer code execution with timeouts
Synchronous vs Asynchronous JavaScript — in detail
Since JavaScript can contain synchronous and asynchronous parts, it becomes important for one to be able to understand which parts are synchronous and which parts are asynchronous.
We’ll examine a simple example together so you understand completely what’s synchronous and what’s asynchronous.
The example we’re going to use is an event listener.
const button = document.querySelector('button')
button.addEventListener('click', function (event) { console.log('Button clicked')})The first line where you select the button with querySelector is synchronous. Here, JavaScript finds the DOM element and assigns it to the button variable immediately.
// This is synchronousconst button = document.querySelector('button')The second line is a bit more complex. We have to break it down into its constituents to get a better understanding of what’s synchronous and asynchronous.
Here’s what it can be broken down into:
button.addEventListener('click', handleClick)
function handleClick(event) { console.log('Button clicked')}Follow closely.
Four things happen when JavaScript evaluates the addEventListener code.
- JavaScript adds runs the
addEventListenermethod. - JavaScript adds the
clickevent andhandleClickmethod to thebutton - JavaScript waits for the
clickevent to occur - When the
clickevent occurs, it runs the code inhandleClick.
Step 1 is synchronous — JavaScript runs the addEventListener method immediately and proceeds to the next task.
Step 2 is also synchronous — this contains 2 parts:
clickis a string so it can be passed intoaddEventListenerimmediately.handleClickis a function. When JavaScript sees this, it looks for thehandleClickfunction and passes it intoaddEventListener, but it doesn’t execute the function.
Step 3 is asynchronous — JavaScript waits for the user to click the button. This is asynchronous because we’re waiting for something to happen.
Step 4 can be defined both ways:
- It is said to be synchronous because the code in
handleClickexecutes immediately when called. - It is said to be asynchronous because we have to wait for Step 3.
Either definition works. But in my opinion, it’s clearer to think that Step 4 is synchronous because that definition is more accurate and precise.
Wrapping Up
In a nutshell, Asynchronous JavaScript happens when you wait for a certain event.
Everything else is synchronous.
With that, we can proceed to the next lesson.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson