Skip to main content

Command Palette

Search for a command to run...

Synchronous vs Asynchronous JavaScript

Updated
5 min read
Synchronous vs Asynchronous JavaScript

"Apply karo, reply ka mat ruko. Kaam karte raho."

You apply for a job. Do you sit and wait for reply?

No. You keep working. Keep applying. Keep learning.

That's asynchronous.

Let me explain.


The Problem: Code That Blocks

Imagine you apply to one company.

Then you just sit. No other work. No other applications. Just waiting for their reply.

Days pass. Nothing.

That's blocking. And it's stupid.


What is Synchronous Code?

Synchronous = one thing at a time. Next line waits for previous to finish.

console.log("Apply to Google");
console.log("Wait for reply");
console.log("Then apply to Amazon");

// Output:
// Apply to Google
// Wait for reply
// (waiting...)
// Then apply to Amazon

You don't do this in real life. So why code like this?


The Problem with Synchronous (Blocking)

function applyToCompany() {
  // Imagine this takes 5 days to get reply
  waitForReply();  // Blocks everything
  return "Offer";
}

console.log("Start applying");
const result = applyToCompany(); // Blocks here for 5 days
console.log(result);
console.log("Apply to next company");

// You just wasted 5 days doing nothing.

That's terrible.


Synchronous Execution

Each step blocks the next. Wasted time.


What is Asynchronous Code?

Asynchronous = start something, don't wait, move on. When reply comes, handle it.

console.log("Apply to Google");

setTimeout(() => {
  console.log("Google replied!");
}, 3000); // Reply comes after 3 days

console.log("Apply to Amazon");
console.log("Work on side project");
console.log("Apply to Microsoft");

// Output:
// Apply to Google
// Apply to Amazon
// Work on side project
// Apply to Microsoft
// (after 3 days) Google replied!

Apply → keep working → reply comes later.


Asynchronous Execution Diagram


Why JavaScript Needs Asynchronous Behavior

JavaScript is single-threaded. One thing at a time.

If it blocks waiting for a job reply — your entire app freezes. Buttons don't work. Screen stuck.

Real-world async tasks in code:

  • Fetching data from API (waiting for server reply)

  • Reading a file

  • Database query

  • User clicking a button

Async = app stays responsive. Like you staying productive while waiting for job replies.


Real Examples

Example 1: setTimeout (async)

console.log("Apply to Google");

setTimeout(() => {
  console.log("Google: We want to interview");
}, 3000);

console.log("Apply to Amazon");
console.log("Solve 5 LeetCode problems");
console.log("Apply to Microsoft");

// Output:
// Apply to Google
// Apply to Amazon
// Solve 5 LeetCode problems
// Apply to Microsoft
// (3 sec later) Google: We want to interview

Example 2: API call (async job application)

console.log("Submitting application...");

fetch("https://api.jobs.com/apply")
  .then(response => response.json())
  .then(data => console.log("Reply:", data));

console.log("Continue applying elsewhere");
console.log("Prepare for interviews");

// Output:
// Submitting application...
// Continue applying elsewhere
// Prepare for interviews
// (later) Reply: { status: "interview" }

Example 3: Blocking vs Non-blocking

// Synchronous (bad - you wait)
function syncApply() {
  const reply = waitForReply(); // Blocks
  console.log(reply);
  console.log("Now apply next");
}

// Asynchronous (good - keep moving)
function asyncApply() {
  submitApplication((reply) => {
    console.log(reply);
  });
  console.log("Apply next"); // Runs immediately
}

How Asynchronous Works Behind the Scenes (Simple)

JavaScript has:

  1. Call Stack — what you're doing right now

  2. Web APIs / Background — slow tasks (waiting for job replies)

  3. Task Queue — pending replies waiting to be handled

  4. Event Loop — moves replies from queue to stack when stack is empty

Call Stack (busy coding) → Submit application → Background (waiting)
                                              ↓
                                        Task Queue (replies)
                                              ↓
Event Loop (waits for stack empty) → handles reply

Analogy:

  • Stack = your current task (coding, applying)

  • Queue = emails from companies you haven't opened yet

  • Event Loop = you checking inbox when free


Problems with Blocking Code

Problem Job Analogy
Wasted time Waiting for one reply instead of applying elsewhere
Lost opportunities Other jobs close while you wait
No growth You didn't upskill during waiting period
Frustration Feeling stuck, unproductive

In code: UI freezes, user can't click, bad experience.


Synchronous vs Asynchronous — Quick Comparison

Synchronous Asynchronous
Execution Wait for reply, then move on Apply, keep working, handle reply later
Blocking Yes No
Productivity Low (wasted waiting) High (parallel work)
Use case Simple math API calls, timers, file reads

When to Use What

Use Synchronous when:

  • Quick calculations (2+2)

  • Must have result before next step (like getting user input on same page)

Use Asynchronous when:

  • Network requests (job applications)

  • File operations

  • Timers / delays

  • Database queries

  • Any slow operation


Common Mistake (Forgetting Async)

let interviewCall;

fetch("/api/apply")
  .then(res => res.json())
  .then(data => {
    interviewCall = data;  // This runs later
  });

console.log(interviewCall); // undefined (runs immediately)

console.log("I'll just wait here"); // Wrong approach

// Fix: Do everything inside .then or use async/await

Async/Await — Modern Way

async function jobSearch() {
  console.log("Applying to Google...");
  
  const reply = await fetch("/api/apply");
  const data = await reply.json();
  
  console.log("Google replied:", data);
  console.log("Now preparing for interview");
}

jobSearch();

Looks synchronous. Runs async. You apply, wait for reply, then proceed — but doesn't block other code outside this function.


Final Thought

In life, you don't wait for one job reply to apply to the next. You keep grinding. You keep growing. JavaScript works the same — async keeps the app responsive while waiting for slow tasks. Apply karo, reply ka mat ruko. Kaam karte raho.