Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Understanding Async JS
Lesson Plan
  1. Introduction
    1. What is Asynchronous JS?
    2. Callbacks are not automatically asynchronous
    3. What is an API?
    4. Evolution of HTTP APIs
    5. Ajax vs Asynchronous JavaScript
  2. Fundamental Concepts
    1. How to fetch resources
    2. Using XHR
    3. Using the Fetch API
    4. Using libraries
    5. Core JavaScript concepts
    6. Understanding Promises
    7. Understanding Async Functions
    8. Understanding the Event Loop
    9. Understanding JSON
    10. Async JS Best Practices
    11. Using Optimistic UI
  3. Understanding Rest API
    1. Diving into Rest APIs
    2. The Rest Architecture
    3. The Rest Structure
    4. Anatomy of a Request
    5. What's an endpoint
    6. Request methods
    7. Understanding request headers
    8. Understanding the request body
    9. Authentication
    10. Basic Authentication
    11. Bearer Authentication
    12. Authenticating with API keys and API Secrets
    13. Cross Origin Resource Sharing
    14. Anatomy of a response
    15. HTTP Statuses
    16. Response headers
    17. The response body
    18. Handling Errors
    19. Reading API documentations
    20. Understanding Curl
  4. Node Basics
    1. Why we are using Node
    2. Command Line Basics
    3. Installing Node
    4. Managing dependencies
    5. Using ES Modules in Node
    6. Using Node
    7. Restarting the server automatically
    8. Sending requests via Node
    9. Protecting API keys and secrets
  5. Building a server
    1. What's a server
    2. Building a simple server with Express
    3. Two kinds of servers
    4. API Server architecture
    5. Handling routes
    6. Understanding Middlewares
    7. Enabling CORS
    8. Receiving data
    9. Sending responses
    10. Handling errors in Express
    11. Using the server as a proxy
    12. Minimum viable server
  6. Open Authorization
    1. Open Authorisation (OAuth)
    2. Authenticating with Access Tokens
    3. Obtaining Access Tokens
    4. Public and Confidential Clients
    5. Authorisation Code Grant
    6. Understanding scope
    7. Proof Key for Code Exchange (PKCE)
    8. PKCE with Twitter
    9. Dynamic state and `code_verifier`
    10. Saving the access token in cookies
    11. Passing access tokens to the browser
    12. Saving the access token in local storage
    13. Refresh Token Grant
    14. Device Code Grant
    15. Client libraries vs REST
  7. Building Social Media Logins
    1. Introduction
    2. Starter Files
    3. Creating SSL certificates
    4. Login in with Github
    5. Github: Saving the token to local storage
    6. Github: Persisting the login status
    7. Login with Twitter
    8. Twitter: Saving the token to local storage
    9. Twitter: Persisting the login status
    10. Twitter: Refreshing the access token
    11. Login with Facebook
    12. Facebook: Saving the token to localStorage
    13. Facebook: Persisting the login status
    14. Facebook: Refreshing the access token
    15. Login with google
    16. Google: Saving the token to localStorage
    17. Google: Refreshing the access token

What is Asynchronous JS?

3m:26s

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:

  1. Send a request to a server
  2. Wait for a response to come back
  3. 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 function
async 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')
One, Two, and Three logged into the console

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 synchronous
const 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.

  1. JavaScript adds runs the addEventListener method.
  2. JavaScript adds the click event and handleClick method to the button
  3. JavaScript waits for the click event to occur
  4. When the click event occurs, it runs the code in handleClick.

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:

  • click is a string so it can be passed into addEventListener immediately.
  • handleClick is a function. When JavaScript sees this, it looks for the handleClick function and passes it into addEventListener, 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 handleClick executes 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.