JS-151-API
  • JavaScript Library
  • 02-DOM-Manipulation
    • Overview
    • DOM Explained
    • Example Setup
    • Selecting Elements
    • Events
    • Complete Example
  • 03-API Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-Intro-To-APIs
      • Intro
      • Client
      • Requests
      • JSON
      • API Endpoints
      • Server
      • Response
      • Statelessness
      • REST
    • 2-Asynchronous-Programming
      • Intro
      • Callbacks
      • Promises
      • Promises Continued
      • Chaining Promises
    • 3-Fetch
      • Fetch Intro
      • Star Wars
        • Star Wars API
        • Star Wars Setup
        • Star Wars JS
      • Random Photo
        • Unsplash
        • Unsplash Setup
        • Unsplash JS
    • 4-Apps
      • 01-New York Times
        • 00-App Intro
        • 01-HTML/CSS/API Key
        • 02-Variables
        • 03-Event Listeners
        • 04-fetchResults
          • 01-fetchResults()
          • 02-preventDefault()
          • 03-fetch() method
          • 04-Dates
        • 05-displayResults
          • 01-Logging JSON
          • 02-Working with JSON
          • 03-Link Display
          • 04-Results Navigation
          • 05-Results Update
          • 06-Keywords
          • 07-Images
        • 06-Pagination
          • 01-Pagination Intro
          • 02-Pagination Functions
        • 07-Next Steps
      • 02-YouTube
        • html
        • youtube.css
        • youtube.js
      • 03-GoogleMaps
        • Setup
        • HTML and CSS files
        • API Key
        • JS Setup
        • Adding Your Location
        • Listeners
        • Custom Options
Powered by GitBook
On this page
  1. 03-API Fundamentals
  2. 2-Asynchronous-Programming

Intro

Previous2-Asynchronous-ProgrammingNextCallbacks

Last updated 6 years ago

JavaScript is known as a single-threaded programming language. We see this played out by JavaScript reading each line of code then implementing that line. What happens when I have my code retrieve some data that isn't explicitly in my code base (ex. an API call?). Well, if JavaScript did not have the ability to run processes asynchronously it would look like this:

As you can see, it took 60ms for our code to fully run through... Yes, we know we are talking in terms of milliseconds, but multiply that with a large number of users, this can quickly turn out to be seconds or, even worse, .

Now let's see how using an async way of thinking can optimize the code base

We see that the thread is not halting because of the external data retrieval. It continues to run, and once the async operation is finished, it then presents us with that information.

Keep in mind that one pitfall of optimization is complexity. Let's investigate further...

This is one of the major benefits of working with asynchronous code. But before we can get into building asynchronous code, let's explore the topic of Callbacks.

minutes
Sync
Async