Understanding Synchronous vs Asynchronous Code
SkillVeris Team
Engineering Team

Synchronous code executes one statement at a time and each line blocks the next until it finishes, while asynchronous code starts a task and moves on without waiting for it to complete.
In this guide, you'll learn:
- JavaScript runs on a single thread, so blocking operations freeze everything, which is why async is essential for I/O.
- The event loop lets JavaScript offload slow work and run a callback later when the result is ready.
- Callbacks, Promises, and async/await are three generations of syntax for handling asynchronous results.
- async/await lets you write asynchronous code that reads top to bottom like synchronous code.
1Synchronous vs Asynchronous: The Core Difference
Synchronous code runs one line at a time, and each operation must finish before the next begins — it blocks. Asynchronous code, by contrast, can start a slow operation and continue running other code, handling the result later when it becomes available. That is the entire distinction: does the program wait, or does it carry on?
This matters most for tasks that take time, like fetching data over a network or reading a file. Synchronous waiting would freeze the whole program; asynchronous handling lets it stay responsive and do other work in the meantime.
2Why It Matters in JavaScript
JavaScript runs on a single thread, meaning it executes one piece of code at a time. If a synchronous operation takes three seconds, nothing else can happen for those three seconds — no clicks handled, no rendering, nothing. In a browser the page appears frozen; in Node a slow request blocks every other request.
- Single thread: one call stack, one thing at a time.
- Blocking sync work freezes the UI or stalls the server.
- Async offloads waiting so the thread stays free.
- This is why almost all I/O in JavaScript is asynchronous by design.
🔑One Thread, No Waiting
Because JavaScript has a single thread, it cannot afford to sit idle waiting for slow operations. Asynchronous handling is how it stays fast on one thread.
3The Event Loop
The event loop is the mechanism that makes single-threaded asynchrony work. When you call something slow like a network request, JavaScript hands it off to the environment, keeps running your other code, and registers a callback. When the operation finishes, its callback is queued and the event loop runs it once the call stack is clear.
You do not manage the event loop directly, but understanding it explains why a setTimeout with zero delay still runs after your current synchronous code, and why async results always arrive in a later tick rather than immediately.
4Callbacks, Promises, and async/await
JavaScript has evolved three ways to handle asynchronous results. Each solves problems in the previous one, and modern code mostly uses the newest.
- Callbacks: pass a function to run when the work finishes. Simple, but nesting them gets messy.
- Promises: an object representing a future value, chained with .then() and .catch().
- async/await: syntax built on Promises that reads like synchronous code.
The Same Task, Three Ways
A single fetch can be written as a callback, a Promise chain, or with await. They do the same thing under the hood — async/await is just the most readable surface over Promises.
fetchData((err, data) => { }) // callback
fetchData().then(data => { }) // Promise
const data = await fetchData() // async/await5async/await in Practice
async/await lets you write asynchronous code that reads top to bottom. Mark a function async, then await any Promise inside it — execution pauses at the await until the value is ready, without blocking the thread. Wrap awaits in try/catch to handle failures the same way you would synchronous errors.
- async function loadUser(id) {
- try {
- const res = await fetch(`/api/users/${id}`)
- const user = await res.json()
- return user
- } catch (err) {
- console.error('Failed to load user', err)
- }
- }
💡await Does Not Block the Thread
Pausing at an await frees the thread to run other code; only the current async function waits. The event loop keeps everything else moving.
6When to Use Sync vs Async
Not everything should be asynchronous. Fast, in-memory work — arithmetic, string manipulation, array operations — is best kept synchronous because there is nothing to wait for and async would only add overhead. Reach for async whenever the operation involves waiting on something external.
- Async: network requests, database queries, file I/O, timers.
- Sync: calculations, data transformations, in-memory lookups.
- Rule of thumb: if it waits on the outside world, make it async.
- Avoid synchronous file or network APIs on a server's hot path.
7Common Mistakes to Avoid
Asynchronous code has a few classic pitfalls.
- Forgetting await, so you work with a pending Promise instead of the value.
- Using a synchronous, blocking API (like fs.readFileSync) on a server request path.
- Awaiting independent operations one by one instead of running them together with Promise.all.
- Ignoring errors by leaving out try/catch or .catch(), so failures pass silently.
- Assuming async code runs immediately — its result always arrives in a later tick.
8Key Takeaways
The sync/async distinction underpins all of JavaScript.
- Synchronous code blocks; asynchronous code starts work and continues.
- JavaScript is single-threaded, so blocking freezes everything.
- The event loop runs callbacks once slow work completes.
- Callbacks, Promises, and async/await are three ways to handle async results.
- Use async for I/O and waiting, sync for fast in-memory work.
9Frequently Asked Questions
Q: What is the difference between synchronous and asynchronous code? A: Synchronous code runs one line at a time and blocks until each finishes, while asynchronous code starts a task and continues without waiting, handling the result later. Async keeps single-threaded JavaScript responsive during slow operations.
Q: Why is JavaScript asynchronous if it is single-threaded? A: Precisely because it is single-threaded. With only one thread, blocking on slow I/O would freeze the entire program, so JavaScript offloads waiting to the environment and uses the event loop to run callbacks when results are ready.
Q: Is async/await better than Promises? A: async/await is built on Promises and does not replace them — it is a cleaner syntax that makes asynchronous code read like synchronous code. Under the hood you are still working with Promises, and sometimes .then() or Promise.all is still the right tool.
Q: What happens if I forget to await a Promise? A: The expression evaluates to the pending Promise object rather than its resolved value, so your code proceeds with the wrong data. You may see [object Promise] or undefined behaviour; always await or chain .then() on Promises you need results from.
Related Reading
Get The Print Version
Download a PDF of this article for offline reading.
About the Publisher
SkillVeris Team
Engineering Team
Our engineering writers turn abstract code concepts into hands-on, project-driven learning experiences.
View all postsRelated Posts
Never miss an update
Get the latest tutorials and guides delivered to your inbox.
No spam. Unsubscribe anytime.