AJAX - synchronous and asynchronous requests
difference between synchronous and asynchronous requests in a clear, simple, and structured way with examples, diagrams, and a comparison table.
1. Synchronous Requests
In a synchronous request, the browser waits for the server to respond before continuing to execute the next lines of code.
How It Works
-
The request is sent to the server.
-
The browser pauses execution until the response is received.
-
The page freezes temporarily if the server takes time to respond.
Key Features
-
Tasks are executed one after another.
-
The browser blocks other operations until the request is completed.
-
Slower user experience when server response time is high.
Example Code (Synchronous JavaScript)
var xhr = new XMLHttpRequest();
xhr.open("GET", "data.txt", false); // false → synchronous
xhr.send();
console.log(xhr.responseText); // Code waits here until data is received
Real-Life Example
-
Imagine standing in a queue at a bank.
You cannot do anything else until the cashier completes your transaction.
2. Asynchronous Requests (AJAX)
In an asynchronous request, the browser does not wait for the server’s response.
The rest of the code continues executing, and the server’s response is handled later when it arrives.
How It Works
-
The request is sent to the server.
-
The browser continues executing other code.
-
When the server responds, a callback or promise handles the data.
Key Features
-
Tasks are executed in parallel.
-
The browser does not block other operations.
-
Better performance and smoother user experience.
Example Code (Asynchronous JavaScript)
var xhr = new XMLHttpRequest();
xhr.open("GET", "data.txt", true); // true → asynchronous
xhr.onload = function() {
console.log(xhr.responseText); // Handled later when data arrives
};
xhr.send();
console.log("This runs without waiting for the response");
Real-Life Example
-
Ordering food at a restaurant:
You place your order and continue talking with friends.
When your order is ready, the waiter brings it to you.
3. Comparison Table
Feature | Synchronous Request | Asynchronous Request (AJAX) |
---|---|---|
Execution | Code runs step by step | Code continues without waiting |
Browser Behavior | Blocks UI until response arrives | Does not block UI |
User Experience | Slower, less interactive | Faster, smoother, more dynamic |
Performance | Less efficient | Highly efficient |
Example Use | File uploads, payment confirmations | Search suggestions, live chat |
AJAX Default | Not used often | Widely used |
4. Simple Visualization
Synchronous Flow
Browser → Sends Request → WAIT... → Gets Response → Continues Execution
-
Blocks other tasks.
Asynchronous Flow (AJAX)
Browser → Sends Request → Continues Other Tasks → Gets Response Later → Updates Page
-
No blocking, smooth experience.
5. Real-Life Examples
Use Case | Synchronous | Asynchronous (AJAX) |
---|---|---|
Login Form | Waits for response → page reload | Submits instantly → no reload |
Search Suggestions | Loads whole page each time | Updates results instantly |
Stock Prices | Refreshes entire page for updates | Updates prices live |
Social Media Feed | Manual refresh needed | Auto-refreshes dynamically |