⬅ Previous Topic
What is Asynchronous Programming?Next Topic ⮕
Callbacks, Promises, and Futures in Programming⬅ Previous Topic
What is Asynchronous Programming?Next Topic ⮕
Callbacks, Promises, and Futures in ProgrammingIn programming, how instructions are executed — whether they block the rest of the code or allow other tasks to run simultaneously — greatly affects program behavior and performance. This leads us to two essential concepts: synchronous and asynchronous execution.
In synchronous execution, tasks are performed one after another, in the exact order they are written. A task must complete before the next one begins. This makes the flow simple and predictable but can lead to delays if one task takes too long.
print("Start")
wait(3 seconds) // Simulating a blocking operation
print("End")
Output:
Start (wait 3 seconds...) End
What happens during the 3-second wait in synchronous code?
Answer: The program is paused and cannot do anything else until the wait finishes. This is called blocking.
In asynchronous execution, tasks are initiated and allowed to complete at a later time, without blocking the rest of the code. This enables programs to remain responsive or perform multiple operations in parallel.
print("Start")
runAfter(3 seconds, function() {
print("Async Task Complete")
})
print("End")
Output:
Start End (wait 3 seconds...) Async Task Complete
Why does "End" print before "Async Task Complete"?
Answer: Because the asynchronous function does not block execution. It schedules the task and continues running the next lines immediately.
Imagine you're cooking two dishes. In synchronous mode, you wait for the water to boil before chopping vegetables. In asynchronous mode, you start boiling the water, then chop vegetables while the water boils — making better use of your time.
print("Reading file...")
data = readFile("data.txt") // blocks until done
print("File content:", data)
Output:
Reading file... (wait for file to load...) File content: [file content here]
print("Reading file...")
readFileAsync("data.txt", function(data) {
print("File content:", data)
})
print("Continue with other tasks")
Output:
Reading file... Continue with other tasks (wait for file to load...) File content: [file content here]
Imagine a program that processes 10,000 images one by one synchronously. What’s a better approach?
Answer: An asynchronous strategy that processes multiple images in parallel or in the background to save time and system resources.
Understanding the difference between synchronous and asynchronous execution is crucial for writing efficient, responsive, and scalable programs. While synchronous code is simpler to follow, asynchronous techniques are essential when dealing with real-world delays such as network latency or file system operations.
⬅ Previous Topic
What is Asynchronous Programming?Next Topic ⮕
Callbacks, Promises, and Futures in ProgrammingYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.