0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>Let's spend a bit more time on the returning of values from asynchronous functions.</p>
1
<p>Let's spend a bit more time on the returning of values from asynchronous functions.</p>
2
<p>In the last lesson, we learned that asynchronous functions never return the result of an asynchronous operation:</p>
2
<p>In the last lesson, we learned that asynchronous functions never return the result of an asynchronous operation:</p>
3
<p>And the only way to get a result is to describe the logic in the callback. Then we should ask what happens if we make a return inside the callback. What will this lead to:</p>
3
<p>And the only way to get a result is to describe the logic in the callback. Then we should ask what happens if we make a return inside the callback. What will this lead to:</p>
4
<p>As a result, nothing changes because this return isn't used by anything. It does not mean that the return instruction is useless in asynchronous code.</p>
4
<p>As a result, nothing changes because this return isn't used by anything. It does not mean that the return instruction is useless in asynchronous code.</p>
5
<p>On the contrary, it's helpful as a way to interrupt code execution and not return the result:</p>
5
<p>On the contrary, it's helpful as a way to interrupt code execution and not return the result:</p>
6
<p>This pattern is called<strong>guard expression</strong>.</p>
6
<p>This pattern is called<strong>guard expression</strong>.</p>
7
<p>All the information above applies to asynchronous functions which we wrote on our own.</p>
7
<p>All the information above applies to asynchronous functions which we wrote on our own.</p>
8
<p>An asynchronous function is any function that has at least one asynchronous operation inside it. Without exception. Even if in addition to the asynchronous operation, it also performs a synchronous operation, for example, text modification. So, every asynchronous function must take a callback as its only way of ordering events and tracking completion.</p>
8
<p>An asynchronous function is any function that has at least one asynchronous operation inside it. Without exception. Even if in addition to the asynchronous operation, it also performs a synchronous operation, for example, text modification. So, every asynchronous function must take a callback as its only way of ordering events and tracking completion.</p>
9
<p>Let's write an asynchronous wrapper function to read the file, which, besides the reading itself, does a little cleaning by removing leading and trailing spaces from the content.</p>
9
<p>Let's write an asynchronous wrapper function to read the file, which, besides the reading itself, does a little cleaning by removing leading and trailing spaces from the content.</p>
10
<p>Since our function is asynchronous, it must take a callback function as an input, and that function will be called at the end of the operation.</p>
10
<p>Since our function is asynchronous, it must take a callback function as an input, and that function will be called at the end of the operation.</p>
11
<p>This function should have the generally accepted signature, with the first parameter as an error and the second as the data itself. Our asynchronous function cannot return data using return:</p>
11
<p>This function should have the generally accepted signature, with the first parameter as an error and the second as the data itself. Our asynchronous function cannot return data using return:</p>
12
<p>This process is recursive, so any function that internally works with an asynchronous function becomes asynchronous and starts taking a callback function as the input.</p>
12
<p>This process is recursive, so any function that internally works with an asynchronous function becomes asynchronous and starts taking a callback function as the input.</p>
13
<p>Why does this happen? Why can't you perform an asynchronous operation internally without returning it outside the function in any way? In this case, you can't:</p>
13
<p>Why does this happen? Why can't you perform an asynchronous operation internally without returning it outside the function in any way? In this case, you can't:</p>
14
<ul><li>Use the result of an asynchronous function because the data comes to the callback function in a different call stack</li>
14
<ul><li>Use the result of an asynchronous function because the data comes to the callback function in a different call stack</li>
15
<li>Find out whether the operation has finished, whether it was successful</li>
15
<li>Find out whether the operation has finished, whether it was successful</li>
16
</ul><p>We'll look at all of this in future lessons.</p>
16
</ul><p>We'll look at all of this in future lessons.</p>