HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>Before we dive into asynchronous programming, let's observe one key point related to code execution.</p>
1 <p>Before we dive into asynchronous programming, let's observe one key point related to code execution.</p>
2 <p>Take a look at this example:</p>
2 <p>Take a look at this example:</p>
3 <p>Each line in the code above runs only when the previous line has already been executed. In programming, this sequential execution of code is called<strong>synchronous</strong>. And the execution of each particular line can be as complex as you like.</p>
3 <p>Each line in the code above runs only when the previous line has already been executed. In programming, this sequential execution of code is called<strong>synchronous</strong>. And the execution of each particular line can be as complex as you like.</p>
4 <p>If you look at the second line, you can see that we call the map() method, which internally calls Math.sqrt(). In real-world applications, the depth of nesting with functions can be enormous, reaching hundreds of levels.</p>
4 <p>If you look at the second line, you can see that we call the map() method, which internally calls Math.sqrt(). In real-world applications, the depth of nesting with functions can be enormous, reaching hundreds of levels.</p>
5 <p>During code execution, this kind of failure creates a<strong>call stack</strong>. Why a stack? Because that's how the code execution process works.</p>
5 <p>During code execution, this kind of failure creates a<strong>call stack</strong>. Why a stack? Because that's how the code execution process works.</p>
6 <p>Each internal call adds the current function inside the stack. That keeps happening until we reach the deepest function possible. Then, when it comes to returning, the stack comes undone. The functions are retrieved from it one by one in reverse order and continue their execution from the moment where the internal function has returned the result:</p>
6 <p>Each internal call adds the current function inside the stack. That keeps happening until we reach the deepest function possible. Then, when it comes to returning, the stack comes undone. The functions are retrieved from it one by one in reverse order and continue their execution from the moment where the internal function has returned the result:</p>
7 <p>Imagine we have a chain of functions<em>one() calls two(), which calls three(), which calls four()</em>:</p>
7 <p>Imagine we have a chain of functions<em>one() calls two(), which calls three(), which calls four()</em>:</p>
8 <p>Then the code execution will look like this:</p>
8 <p>Then the code execution will look like this:</p>
9 <p>one =&gt; two =&gt; three =&gt; four =&gt; three =&gt; two one</p>
9 <p>one =&gt; two =&gt; three =&gt; four =&gt; three =&gt; two one</p>
10 <p>So, first, there is a dive to the nested call, then a climb up to the first function in the call stack.</p>
10 <p>So, first, there is a dive to the nested call, then a climb up to the first function in the call stack.</p>
11 <p>As developers, we see the call stack every day in the error output. A<strong>backtrace</strong>is nothing more than the call stack written in reverse order. To illustrate, let's make a mistake in the third line of our code:</p>
11 <p>As developers, we see the call stack every day in the error output. A<strong>backtrace</strong>is nothing more than the call stack written in reverse order. To illustrate, let's make a mistake in the third line of our code:</p>
12 <p>Let's observe its running and output. The code is in the<em>index.js</em>:</p>
12 <p>Let's observe its running and output. The code is in the<em>index.js</em>:</p>
13 <p>It is important to understand that the call stack grows only when calls go further in depth. You can see this in the output: the backtrace does not include the first and second lines, it describes the sequence starting from the filter call and beyond.</p>
13 <p>It is important to understand that the call stack grows only when calls go further in depth. You can see this in the output: the backtrace does not include the first and second lines, it describes the sequence starting from the filter call and beyond.</p>
14 <p>Like in other languages, the exception mechanism in JavaScript relies entirely on the existence of a call stack. Moreover, an exception mechanism makes it easier to unwind this stack. Any exception that arises moves up the call stack until it encounters a<strong>try/catch</strong>construct or until the call stack runs out.</p>
14 <p>Like in other languages, the exception mechanism in JavaScript relies entirely on the existence of a call stack. Moreover, an exception mechanism makes it easier to unwind this stack. Any exception that arises moves up the call stack until it encounters a<strong>try/catch</strong>construct or until the call stack runs out.</p>
15 <p>The declaration of the predicate() function contains an error. Even though it is outside the try/catch block, an error will still be caught inside this function since predicate() is called<strong>internally</strong>along the chain:</p>
15 <p>The declaration of the predicate() function contains an error. Even though it is outside the try/catch block, an error will still be caught inside this function since predicate() is called<strong>internally</strong>along the chain:</p>
16 <p>There are some tools for visualizing the call stack. Usually such tools help in<strong>profiling</strong>, the process of finding bottlenecks to speed up the application.</p>
16 <p>There are some tools for visualizing the call stack. Usually such tools help in<strong>profiling</strong>, the process of finding bottlenecks to speed up the application.</p>
17 <p>But everything changes when it comes to asynchronous code. More on this we will discuss later in the course.</p>
17 <p>But everything changes when it comes to asynchronous code. More on this we will discuss later in the course.</p>