Synchronous vs asynchronous JavaScript: what is the difference?
BeginnerJavaScript interview questionBy UIRound, updated 17 November 2026
Short answer
Synchronous code runs one statement at a time, and each statement must finish before the next starts, so a slow step blocks everything after it. Asynchronous code starts an operation, such as a network request or a timer, and carries on; the result is handled later by a callback, a promise or await. JavaScript runs your code on a single thread, so asynchronous APIs are how it waits without freezing.
Synchronous code
console.log("A");
const total = [1, 2, 3].reduce((sum, n) => sum + n, 0);
console.log(total); // 6
console.log("B");
// Output: A, 6, B, always in that orderEvery line runs to completion, in order, on the call stack. This is simple to reason about: when a line finishes, its result is ready. Most of the code you write is synchronous, including loops, array methods and ordinary function calls.
The problem is waiting. If a synchronous step takes a long time, nothing else can run:
const start = Date.now();
while (Date.now() - start < 3000) {} // busy-wait for 3 seconds
console.log("done");In a browser, this freezes the page for three seconds. Clicks, typing, scrolling and rendering all wait, because they need the same main thread. In Node, the server cannot answer any other request in that time. Synchronous APIs that wait for I/O, such as fs.readFileSync in Node, have the same effect while they wait.
Asynchronous code
console.log("A");
setTimeout(() => console.log("C"), 0);
console.log("B");
// Output: A, B, CsetTimeout does not pause the code. It asks the runtime to call the function later and returns at once, so B prints first. Even with a delay of 0, the callback cannot run until the current code has finished.
The waiting itself happens outside your JavaScript. The browser or Node handles the timer, the network socket or the file read, while your code keeps running or the thread sits idle. When the work completes, the runtime queues your callback, and it runs when the call stack is empty. The JavaScript event loop guide explains that queueing in detail, including why promise callbacks run before timers.
Comparison
| Synchronous | Asynchronous | |
|---|---|---|
| When the result is ready | When the line finishes | Later, in a callback, .then or after await |
| Blocks the thread while waiting | Yes | No |
| Order of execution | Exactly the order written | Code after the call runs before the result arrives |
| Error handling | try/catch around the call |
.catch(), or try/catch around await |
| Typical examples | Maths, loops, array methods, DOM reads | fetch, timers, events, file and database I/O |
Three ways to write asynchronous code
The same task, reading JSON from a server, in the three styles you will meet:
// 1. Callbacks: pass a function to run when the work is done
function loadUser(id, callback) {
setTimeout(() => callback(null, { id, name: "Asha" }), 100);
}
loadUser(1, (error, user) => {
if (error) return console.error(error);
console.log(user.name);
});
// 2. Promises: an object that will hold the result
fetch("/api/user/1")
.then((res) => res.json())
.then((user) => console.log(user.name))
.catch((error) => console.error(error));
// 3. async/await: promise code written in a synchronous-looking style
async function showUser() {
try {
const res = await fetch("/api/user/1");
const user = await res.json();
console.log(user.name);
} catch (error) {
console.error(error);
}
}Callbacks came first and nest badly when steps depend on each other; see callbacks and callback hell. Promises represent a future value you can chain and combine; see what is a Promise. async/await is built on promises and reads top to bottom; the trade-offs are in async/await vs promises.
Asynchronous is not the same as parallel
JavaScript on the main thread never runs two pieces of your code at the same time. Asynchronous code lets the thread do other work while it waits for something outside JavaScript. It does not make heavy computation faster.
async function heavy() {
let sum = 0;
for (let i = 0; i < 1e9; i++) sum += i; // still blocks: this is plain synchronous work
return sum;
}Marking a function async only means it returns a promise. Its body runs synchronously until the first await, so this loop blocks just as it would in a normal function. For CPU-heavy work, split it into smaller pieces or move it to a Web Worker (or a worker thread in Node), which really does run on another thread.
Practise it
Predict the event loop order of setTimeout, promises, queueMicrotask and await checks that you know when async callbacks run. Sequential versus parallel awaits, Predict which errors try/catch catches, Implement promisify() and Predict the log order of await inside forEach cover the mistakes listed below.
Common follow-up questions
If JavaScript is single-threaded, how can it do many things at once?
Your JavaScript runs on one thread, but the runtime around it is not limited to one. Timers, network requests and file reads are handled by the browser or Node, and only their callbacks come back to your thread. So many operations can be in progress at once, while your code handles their results one at a time.
Why does try/catch not catch an error in a setTimeout callback?
try/catch not catch an error in a setTimeout callback?By the time the callback runs, the try block has already finished. The error is thrown on a later turn, with no try around it. Put the try/catch inside the callback, or use promises and await so the error flows back to a try that is still waiting.
Why do two awaits in a row take twice as long?
awaits in a row take twice as long?Each await waits for its promise before the next line starts. If the requests do not depend on each other, start both first and wait together: const [a, b] = await Promise.all([getA(), getB()]).
Can I make asynchronous code synchronous?
Not on the main thread, and you should not try. You cannot block until a promise resolves. Use await inside an async function, which pauses that function without blocking the thread.
Mistakes to avoid
Expecting a value from an async call on the next line, for example returning a result from inside a callback and reading it outside
Forgetting
await, so you get a pending promise instead of the dataUsing
awaitinsideforEachand expecting the loop to waitUse
for...oforPromise.all.Assuming
setTimeout(fn, 0)runs immediatelyIt runs after the current code and after queued promise callbacks.
Thinking
asyncmoves heavy work off the main threadAwaiting independent requests one after another when they could run together
Sources
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Predict the event-loop order of
setTimeout, promises,queueMicrotaskandawaitEasy15 minFree - Sequential versus parallel awaits: predict how long each snippet takes, then fix the slow onesMedium30 minFree
- Predict which errors
try/catchcatches: timers, async callbacks, unawaited promises andfinallyMedium25 min - Implement
promisify()for callback-based APIsMedium35 min - Predict the log order of
awaitinsideforEach,for...of,mapwithPromise., andall reduceMedium25 min