Course

Synchronous / Asynchronous

In this lesson, you'll learn about the differences between synchronous and asynchronous JavaScript. Understanding these concepts is crucial for writing efficient and responsive code, especially when dealing with tasks like data fetching and user interactions.

Synchronous JavaScript

Synchronous JavaScript is code that is executed line by line, with each task completing instantly. There is no delay in the execution of tasks for these lines of code.

Here's an example of synchronous JavaScript:

const functionOne = () => {
    console.log('Function One');

    functionTwo();

    console.log('Function One, Part 2');
}

const functionTwo = () => {
    console.log('Function Two');
}

functionOne();

// Output:
// Function One
// Function Two
// Function One, Part 2
  • The code executes in a straightforward manner.
  • First, is logged.
  • Then, is invoked, logging .
  • Finally, back in , is logged.
  • The execution is linear and predictable.

Asynchronous JavaScript

Now, let's explore asynchronous JavaScript by modifying the functions:

const functionOne = () => {
    console.log('Function One'); // 1

    functionTwo();

    console.log('Function One, Part 2'); // 2
}

const functionTwo = () => {
    setTimeout(() => console.log('Function Two'), 2000); // 3
}

functionOne();

// Output:
// Function One
// Function One, Part 2
// (after a two-second delay)
// Function Two
  • In , instead of a normal , we use to simulate a delay, similar to fetching data from a server.
  • The function schedules to be logged after a 2000-millisecond delay.
  • When you run the script, and are logged immediately.
  • After a two-second delay, is logged.

Why doesn't JavaScript wait?

  • JavaScript is designed to be non-blocking. If the engine waited for every time-consuming task, it could lead to unresponsive applications, especially if users interact with the webpage during these delays.
  • This non-blocking behavior allows the JavaScript engine to continue executing other code while waiting for asynchronous tasks to complete.

What is asynchronous JavaScript?

Asynchronous JavaScript involves code where some tasks take time to complete. These tasks run in the background while the JavaScript engine continues executing other lines of code. When the result of the asynchronous task is available, it is then used in the program.

Key Takeaways

  • Synchronous JavaScript: Executes code line by line, waiting for each task to complete before moving on.
  • Asynchronous JavaScript: Allows tasks to run in the background, enabling the engine to continue executing other code. This is essential for handling tasks like network requests without freezing the application.

Understanding the difference between synchronous and asynchronous JavaScript is vital for building applications that are both efficient and responsive. As we progress, you'll learn more about handling asynchronous operations using promises, async/await, and other techniques.

Loading...

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Async JavaScript & Callbacks Part 1