Skip to content

Commit 09b1b59

Browse files
committed
learn briefly about asynchronous code execution in JS
1 parent 9c7cf6a commit 09b1b59

File tree

1 file changed

+71
-2
lines changed

1 file changed

+71
-2
lines changed

Async-JS-Promises-and-Callbacks/README.md

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
| Contents |
44
| :--- |
55
| [Understanding Synchronous Code Execution ("Sync Code")](#understanding-synchronous-code-execution-sync-code) |
6+
| [Understanding Asynchronous Code Execution ("Async Code")](#understanding-asynchronous-code-execution-async-code) |
67

78
## Understanding Synchronous Code Execution ("Sync Code")
89

910
To delve into the concept of synchronous code execution and how JavaScript operates, let's start by understanding the fundamental nature of JavaScript as a single-threaded language.
1011

1112
In the early stages of this course, we touched upon the fact that JavaScript is single-threaded. This notion might not have had significant implications until now, but it's essential to grasp its significance. Being single-threaded means that JavaScript is capable of executing only one task at any given time. This is in contrast to multi-threaded languages, which can execute multiple tasks simultaneously.
1213

13-
<img src="https://drive.google.com/uc?export=view&id=1YV3n6ASkAcrBaso24dj-SsacNSFTvz8R" alt="academind slide">
14+
<img src="https://drive.google.com/uc?export=view&id=1YV3n6ASkAcrBaso24dj-SsacNSFTvz8R" width="600" height="390" alt="academind slide">
1415

1516
Consider a scenario where we have a sequence of code operations. We log something to the console, call a function, modify an element (like disabling a button), and then invoke another function. In the realm of JavaScript's single-threaded nature, these steps occur sequentially. They are executed one after the other in a linear manner. JavaScript will log to the console, call the function, carry out the required actions within that function, disable the button, and then proceed to call the subsequent function. It's essential to emphasize that these actions unfold sequentially, not concurrently.
1617

@@ -31,4 +32,72 @@ button.addEventListener('click', trackUserHandler);
3132

3233
This ordering is crucial, especially when dealing with event listeners. We depend on this sequence to ensure that the button is selected and available before we add an event listener to it. This reliance on order is upheld by JavaScript's single-threaded nature. In a hypothetical multi-threaded scenario, tasks could potentially occur simultaneously, creating uncertainty about the availability of elements for further operations. However, JavaScript's single-threaded architecture guarantees this order.
3334

34-
Understanding synchronous code execution and the single-threaded nature of JavaScript provides insight into how the language processes tasks sequentially and maintains a predictable flow of execution.
35+
Understanding synchronous code execution and the single-threaded nature of JavaScript provides insight into how the language processes tasks sequentially and maintains a predictable flow of execution.
36+
37+
## Understanding Asynchronous Code Execution ("Async Code")
38+
39+
In the previous lecture, we explored the concept of synchronous code execution, where each operation is executed sequentially. Now, let's delve into a potential downside of this approach and how asynchronous code execution addresses these challenges.
40+
41+
Asynchronous code execution is a fundamental concept in JavaScript that allows tasks to be executed independently, without blocking the main execution flow. This is crucial for tasks that may take longer to complete, such as fetching data from a server or waiting for user input, without causing the entire program to become unresponsive. Let's dive into this concept with a practical example.
42+
43+
Consider a scenario where you want to simulate fetching data from a server and updating the UI once the data is received. Here's how you might approach this using asynchronous code:
44+
45+
```javascript
46+
console.log("Start");
47+
48+
// Simulate fetching data from a server
49+
setTimeout(() => {
50+
const data = { id: 1, name: "John Doe" };
51+
console.log("Data Received:", data);
52+
53+
// Update the UI with the received data
54+
updateUI(data);
55+
}, 2000); // Simulating a 2-second delay
56+
57+
console.log("End");
58+
59+
function updateUI(data) {
60+
console.log("Updating UI with:", data.name);
61+
}
62+
```
63+
64+
**Output**
65+
66+
```
67+
Start
68+
End
69+
Data Received: { id: 1, name: "John Doe" }
70+
Updating UI with: John Doe
71+
```
72+
73+
In this example, the code follows these steps:
74+
75+
1. The initial "Start" and "End" logs are synchronous and executed immediately.
76+
77+
2. The [`setTimeout()`](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) function simulates fetching data from a server by using a callback function. The callback is scheduled to execute after a delay of 2000 milliseconds (2 seconds). However, JavaScript doesn't wait for this delay to complete; it continues executing the next line of code immediately.
78+
79+
3. The "End" log is printed while the timer is still running. This demonstrates the non-blocking nature of asynchronous code execution. JavaScript doesn't pause the execution flow and wait for the `setTimeout` to finish.
80+
81+
4. After approximately 2 seconds, the timer expires, and the callback function is executed. The data is logged, and the `updateUI` function is called to update the UI with the received data.
82+
83+
5. The "Updating UI with: John Doe" log demonstrates that the UI update occurred after the data was received.
84+
85+
**Key Takeaways:**
86+
87+
- Asynchronous code execution allows tasks to run independently, preventing the main thread from blocking and ensuring the application remains responsive.
88+
89+
- With the `setTimeout` example, it schedules a task to be executed after a specified delay but JavaScript continues executing subsequent code lines without waiting for the timer to finish. Note that, the browser handles the timer independently from the JavaScript code execution. This means that the browser sets up the timer and manages its countdown without blocking the rest of the script. Once the timer is done, the browser notifies JavaScript, which then executes the specified callback function.
90+
91+
- Callback functions are often used in asynchronous operations to define what should happen once the task is completed.
92+
93+
- In the example, the UI update occurs after the data is received, showcasing the efficient handling of asynchronous tasks without blocking the main program flow.
94+
95+
Asynchronous code execution is essential for creating smooth and responsive web applications that can handle time-consuming tasks without compromising user experience.
96+
97+
Readings:
98+
99+
- [Understanding Asynchronous JavaScript](https://blog.bitsrc.io/understanding-asynchronous-javascript-the-event-loop-74cd408419ff)
100+
101+
- [How does asynchronous JavaScript work behind the scenes?](https://dev.to/vinaykishore/how-does-asynchronous-javascript-work-behind-the-scenes-4bjl#:~:text=Asynchronous%20code%3A&text=I.e.%2C%20the%20code%20is%20executed,task%20to%20finish%20its%20work.)
102+
103+
- [How JavaScript works: Event loop and the rise of Async programming + 5 ways to better coding with async/await](https://medium.com/sessionstack-blog/how-javascript-works-event-loop-and-the-rise-of-async-programming-5-ways-to-better-coding-with-2f077c4438b5)

0 commit comments

Comments
 (0)