Skip to main content

Command Palette

Search for a command to run...

Synchronous vs Asynchronous JavaScript

Updated
4 min read

What is Synchronous code?

Synchronous execution means that your code runs line by line, top to bottom. Each operation must finish completely before the next one can start.

Example:

console.log("1. Wake up");
console.log("2. Brush teeth");
console.log("3. Make coffee");

Output:

1. Wake up
2. Brush teeth
3. Make coffee

Synchronous code is clean and predictable where each line executes and finishes before the next one begins.

But problem arises when one of the tasks takes too long i.e Blocking Code. While a task is doing it's thing the nexts tasks are frozen. Some real world examples of blocking code are:

  • API Calls

  • Reading Large Files

  • Database Queries

The solution to this is Asynchronous Code

What is Asynchronous code?

Asynchronous execution (or non-blocking code) means start a task, if it's long-running; set it aside, and continue running the rest of the code. Once the long task finishes, come back and handle the result.

Example:

console.log("1. Order coffee");

setTimeout(() => {
    console.log("2. Coffee is ready!"); // This takes 3 seconds
}, 3000);

console.log("3. Chat with a friend");

Output:

1. Order coffee
3. Chat with a friend
2. Coffee is ready!

The Secret of JavaScript. How JS pull this of?

JavaScript is a single-threaded, non-blocking, asynchronous concurrent language.

To manage asynchronous operations without freezing the main thread, the JavaScript runtime relies on a specific architecture composed of four main interconnected parts.

  1. The Call Stack (Synchronous Execution)
    Technical Definition: A Last-In-First-Out (LIFO) data structure that records where the application is in its execution.
    How it works: When a function is invoked, an execution context (a "frame") is pushed onto the stack. When the function returns, it is popped off the stack. Because JavaScript is single-threaded, it has only one Call Stack, meaning it can only execute one piece of code at a time. If it encounters an asynchronous operation (like a network request), it does not wait for it to finish; instead, it delegates it to the Web APIs.

  2. Web APIs (The Host Environment)
    Technical Definition: A set of interfaces provided by the host environment (such as the web browser or the Node.js runtime) rather than the JavaScript engine itself.
    How it works: These APIs (which include setTimeout, fetch, DOM events, and Geolocation) run on separate threads managed by the browser. When the Call Stack passes an asynchronous task to a Web API, the browser handles the heavy lifting in the background. Once the background task completes (e.g., a timer expires or HTTP data is downloaded), the Web API wraps the associated callback function and sends it to the Task Queue.

  3. The Task Queue (Callback Queue)
    Technical Definition: A First-In-First-Out (FIFO) data structure that holds callback functions waiting to be executed.
    How it works: As Web APIs finish their background tasks, they push the corresponding callbacks into this queue. Code in the Task Queue does not execute immediately; it simply waits in line for its turn on the main thread. (Note: In modern JavaScript, there is also a higher-priority "Microtask Queue" used for Promises, which resolves before the standard Task Queue).

  4. The Event Loop (The Orchestrator)
    Technical Definition: A continuous process that coordinates the execution of code between the Call Stack and the Task Queue.
    How it works: The Event Loop has a single, strictly defined job: it continuously monitors the Call Stack and the Task Queue. If the Call Stack is not empty, the Event Loop does nothing. However, the exact millisecond the Call Stack becomes completely empty, the Event Loop takes the first callback in the Task Queue and pushes it onto the Call Stack for execution.

This architecture is known as the Event-Driven, Non-Blocking I/O model. By offloading time-consuming tasks to Web APIs and using the Event Loop to manage callbacks, JavaScript can handle complex, data-heavy operations seamlessly without ever blocking the main thread or freezing the user interface.

JavaScript Fundamentals for Developers

Part 12 of 22

Learn JavaScript from the ground up with clear explanations and practical examples. This series covers core JavaScript concepts like variables, arrays, functions, loops, objects, and modern ES6 features to help you build a strong foundation in JavaScript.

Up next

A Deep Dive into this, Call(), Apply(), and Bind()

What this Means in JavaScript In JavaScript, this refers to the object associated with the current function invocation. The key rule is, the value of this is determined by how a function is called, no