Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Introduction to Asynchronous JavaScript | Asynchronous JavaScript and APIs
Advanced JavaScript Mastery
course content

Course Content

Advanced JavaScript Mastery

Advanced JavaScript Mastery

1. Classes
2. DOM Manipulation
3. Events and Event Handling
4. Asynchronous JavaScript and APIs

bookIntroduction to Asynchronous JavaScript

What is Asynchronous Programming?

Asynchronous programming allows your code to execute non-blocking operations. In contrast to synchronous programming—where each operation waits for the previous one to complete before continuing—asynchronous programming allows other tasks to proceed without waiting for a previous task to finish.

This is crucial in web development, where you may need to perform tasks that take time, such as fetching data from an API, waiting for user input, or setting timers.

Differences Between Synchronous and Asynchronous Behavior

Synchronous Programming

In synchronous programming, tasks are executed one after another. Each task must complete before the next one starts. If one task takes a long time (e.g., a large file upload), it blocks all subsequent tasks, which can make the application unresponsive.

123
console.log('Task 1: Start'); for (let i = 0; i < 1000000000; i += 1) {} // Simulate a long-running task console.log('Task 2: After long task');
copy

In this example, Task 2 will only run after the long-running task (the loop) is finished. This blocks the code execution and can freeze the browser.

Asynchronous Programming

In asynchronous programming, tasks can be initiated and then executed later without blocking other code. This allows other tasks to continue while waiting for long-running operations (e.g., data fetching) to complete. It ensures that tasks like network requests or timers do not stop your application's flow of other operations.

html

index

css

index

js

index

copy

In this example, Task 1 runs immediately, Task 3 also runs immediately, and Task 2 executes after 2 seconds. Asynchronous behavior allows the program to continue without waiting for Task 2 to complete.

Real-World Examples of Asynchronous Operations

Fetching Data from an API

One of JavaScript's most common asynchronous operations is fetching data from a remote server using APIs. JavaScript requests data from an API, but the rest of the code continues to execute instead of waiting for the server's response. Once the data is available, it's processed using a callback or promise.

html

index

css

index

js

index

copy
  • Synchronous Code: The paragraph with id="syncMessage" demonstrates that synchronous code runs immediately after starting the asynchronous fetch. It shows that the program doesn't pause while waiting for the API data;
  • Asynchronous Fetch: Once the data is fetched, the paragraph with id="apiOutput" is updated, demonstrating the completion of the asynchronous task.

Timers (setTimeout and setInterval)

JavaScript's setTimeout() and setInterval() are commonly used to schedule tasks to run after a delay or at regular intervals. These functions don't block the execution of other code. The tasks they trigger occur after a specified delay or interval while the rest of the code continues to run.

html

index

css

index

js

index

copy
  • Synchronous Code: The paragraph with id="syncMessage" updates immediately, showing that the synchronous part of the code runs without waiting for the timer;
  • Asynchronous Timer: After 3 seconds, the setTimeout() callback completes and updates the id="timerOutput" paragraph to show that the timer has finished.

Handling User Input Events

JavaScript waits asynchronously for events to occur without blocking other code when dealing with user interactions (such as clicks, form submissions, or key presses). Event listeners are non-blocking, meaning the rest of the program can continue executing while waiting for user input.

html

index

css

index

js

index

copy
  • Synchronous Code: The paragraph with id="syncMessage" shows that synchronous code runs immediately after setting up the event listener. It doesn't wait for the user to click the button;
  • Asynchronous Event Handling: The button's event listener is triggered when the user clicks, updating the id="eventOutput" paragraph.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 1
We're sorry to hear that something went wrong. What happened?
some-alt