Skip to content

Async vs Sync in JavaScript: What Every Web Developer Should Know

Published:
Async vs Sync in JavaScript: What Every Web Developer Should Know

As a JavaScript developer who started out in the Python 2 days (before async was a concern), I’ve always found the difference between synchronous (sync) and asynchronous (async) functions a bit confusing. If you’re in the same boat, this post is for you!

The Basics: Synchronous vs Asynchronous Functions

Synchronous functions run one line at a time, in order. Each line waits for the previous one to finish. If a function takes a long time (like a big calculation), everything else waits.

function syncFunction() {
  console.log("Start");
  // Simulate a long task
  for (let i = 0; i < 1e9; i++) {}
  console.log("End");
}

syncFunction();
console.log("After syncFunction");

Output:

Start
End
After syncFunction

Asynchronous functions can start a task and move on without waiting for it to finish. In JavaScript, this is done with callbacks, Promises, and async/await.

function asyncFunction() {
  console.log("Start");
  setTimeout(() => {
    console.log("End");
  }, 1000);
}

asyncFunction();
console.log("After asyncFunction");

Output:

Start
After asyncFunction
End

Does await Block the Main Thread?

No. When you use await inside an async function, it only pauses that function until the Promise resolves. The rest of your code (including UI updates and event handlers) keeps running.

async function demo() {
  console.log("Start");
  await new Promise(resolve => setTimeout(resolve, 1000));
  console.log("End");
}

demo();
console.log("After demo");

Output:

Start
After demo
End

So, await is a way to write asynchronous code that looks synchronous, but it does not block the main thread or freeze the UI.

Why Aren’t All Functions async?

Best practice: Use async only when you need to await something inside the function. Keep functions synchronous when they don’t need to wait for anything.



Next Post
Server Actions vs API Routes: Modern Data Mutations in Next.js & Remix