Sync vs Async – The Core Difference

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:
Keyword | What it does |
async | Makes a function return a Promise |
await | Pauses 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:
Feature | Synchronous | Asynchronous |
Execution Order | One after another | Can skip ahead while waiting |
Blocking? | Yes | No |
Slows down others? | Yes (everything waits) | No (others continue running) |
Examples | Regular functions, loops | setTimeout, 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!
Subscribe to my newsletter
Read articles from Godson Prakasia directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
