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

Using libraries

2m:18s

We usually use libraries over raw XHR and Fetch because libraries can make it much easier for us to code.

I’m not going to share how it makes it easier here because there are a lot of details. You’re going to find out what these details are in the next chapter.

The library we’re going to use

The most popular libraries are Axios and ky.

But we’re not going to use either of those in this course.

We’re going to use a library called zlFetch. We use it because it’s the library I’m most familiar with. (I made the library and I use it in all my projects).

Note

Don’t worry if you’re not using a popular library in this course. These libraries are all similar in principle so you can switch to a different library anytime.

By the way, as you go through the lessons, you may begin to like zl-fetch over the other libraries more and more. That’s because I put a ton of work into designing an easy-to-use API 🙂.

Installing zl-fetch

If you’re using a browser, you can download zlFetch with an import statement.

To use an import statement, you need to make sure your script has type set to module

<script type="module">
import zlFetch from 'https://cdn.jsdelivr.net/npm/zl-fetch/src/index.js'
</script>

If you’re using Node, or if you’re using Frameworks like Astro, React, Vue, and Svelte, you can install zlFetch via npm.

Terminal window
npm install zl-fetch --save
import zlFetch from 'zl-fetch'

Feel free to read the docs for more information on zlFetch. It will be a handy guide when you’re tackling more advanced stuff later.

Fetching with zlFetch

Sending a basic request with zlFetch is the same as sending a basic request with fetch — you write the url and options you wish to use:

zlFetch('https://link-to-resource', options)

When receiving responses, zlFetch makes it easy for you by automatically converting the data into a JavaScript object.

To get the data, you just have to look at the body property.

zlFetch('https://link-to-resource', options).then(response => {
const data = response.body
})

Fetch vs zlFetch

Here’s a basic comparison between fetch and zlFetch.

// Fetch
fetch('https://link-to-resource', options)
.then(response => response.json())
.then(data => {
// Do something with Data
})
// zlFetch
zlFetch('https://link-to-resource', options).then(response => {
const data = response.body
// Do something with Data
})

This is a basic example of how libraries can simplify the fetching process.

Most libraries do many other things on top of this basic example I’m showing you right now. We’ll explore what are some of these things are later in the Rest API chapter.

For now, let’s take a look at the core JavaScript concepts you must understand before you look at Rest APIs.