Sync vs Async – The Core Difference

Godson PrakasiaGodson Prakasia
3 min read

An async function in JavaScript is a special kind of function that lets you write asynchronous code (like API calls or timers) in a clean, readable way — using await.


🧠 Basic Idea:

An async function always returns a Promise, even if you don’t explicitly return one.

Inside it, you can use the await keyword to pause the function until a Promise is done (resolved or rejected).


✅ Basic Example:

async function fetchData() {
  const response = await fetch('https://api.example.com/data');
  const data = await response.json();
  console.log(data);
}

• async makes the function asynchronous.

• await pauses the function until the fetch is complete.

• It avoids messy .then() chaining.


🔁 Compared to regular Promise style:

Without async/await:

fetch('https://api.example.com/data')
  .then(res => res.json())
  .then(data => console.log(data));

With async/await:

async function getData() {
  const res = await fetch('https://api.example.com/data');
  const data = await res.json();
  console.log(data);
}

Much cleaner, right?


🔴 Error Handling with async/await:

async function getData() {
  try {
    const res = await fetch('https://api.example.com/data');
    const data = await res.json();
    console.log(data);
  } catch (error) {
    console.error('Error:', error);
  }
}

✅ Summary:

KeywordWhat it does
asyncMakes a function return a Promise
awaitPauses execution until a Promise resolves


⚖️ Sync vs Async – The Core Difference

🔵 Synchronous (Sync)

• Executes code line by line, one after the other.

• Each line waits for the previous one to finish.

• If something takes time (like a long calculation or file read), everything else gets blocked.

Example:

console.log("1");
console.log("2");
console.log("3");

✅ Output:

1
2
3

🟣 Asynchronous (Async)

• Executes code non-blocking.

• Doesn’t wait for slow tasks (like API calls, file loading, etc.).

• Can use setTimeout, fetch, async/await, Promises, etc.

• Let’s the program keep running while waiting for something to finish in the background.

Example:

console.log("1");

setTimeout(() => {
  console.log("2");
}, 1000);  // delay of 1 second

console.log("3");

✅ Output:

1
3
2

setTimeout is async — it runs after the rest of the sync code is done.


✅ Summary Table:

FeatureSynchronousAsynchronous
Execution OrderOne after anotherCan skip ahead while waiting
Blocking?YesNo
Slows down others?Yes (everything waits)No (others continue running)
ExamplesRegular functions, loopssetTimeout, fetch, async/await, Promises

🔧 When to use async?

• API calls

• File reading

• Waiting on animations

• Anything that takes time and you don’t want to block the app


Let me know if you want a hands-on example or a real-world use case!

0
Subscribe to my newsletter

Read articles from Godson Prakasia directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Godson Prakasia
Godson Prakasia