0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>If there are promises in the project, the whole code will work through them. Unfortunately, not all libraries have an interface for dealing with promises, so they operate the old-fashioned way, through callbacks. In such cases, we need to wrap functions or promise them.</p>
1
<p>If there are promises in the project, the whole code will work through them. Unfortunately, not all libraries have an interface for dealing with promises, so they operate the old-fashioned way, through callbacks. In such cases, we need to wrap functions or promise them.</p>
2
<p>To create promises, use the Promise builder:</p>
2
<p>To create promises, use the Promise builder:</p>
3
<p>Promises expect that a function will be called when it is created. Within that function, we should execute the asynchronous operation on the callback, that we want to turn into a promise.</p>
3
<p>Promises expect that a function will be called when it is created. Within that function, we should execute the asynchronous operation on the callback, that we want to turn into a promise.</p>
4
<p>Promises throw two callbacks into this function:</p>
4
<p>Promises throw two callbacks into this function:</p>
5
<ul><li>resolve - should be called if the asynchronous operation is completed successfully. As an input, it takes the result of the operation</li>
5
<ul><li>resolve - should be called if the asynchronous operation is completed successfully. As an input, it takes the result of the operation</li>
6
<li>reject - should be called in case of an error. As an input, it takes the error</li>
6
<li>reject - should be called in case of an error. As an input, it takes the error</li>
7
</ul><p>These functions take exactly one argument, passed:</p>
7
</ul><p>These functions take exactly one argument, passed:</p>
8
<ul><li>To then, if there is some data</li>
8
<ul><li>To then, if there is some data</li>
9
<li>Tocatch, if there is an error</li>
9
<li>Tocatch, if there is an error</li>
10
</ul><p>It's enough to call at least one of these functions. It may be necessary to create a promise that always ends successfully, and you can easily do this without ever calling reject.</p>
10
</ul><p>It's enough to call at least one of these functions. It may be necessary to create a promise that always ends successfully, and you can easily do this without ever calling reject.</p>
11
<p>Finally, the new new Promise() construct returns a real promise we can work with the way we're used to:</p>
11
<p>Finally, the new new Promise() construct returns a real promise we can work with the way we're used to:</p>
12
<p>What if we have to wrap two asynchronous operations or more? We should wrap each of them independently.</p>
12
<p>What if we have to wrap two asynchronous operations or more? We should wrap each of them independently.</p>
13
<p>In other words, one asynchronous operation means one constructor new Promise. By the way, you can automate this task. There's a function built into node.js that makes promises out of asynchronous functions:</p>
13
<p>In other words, one asynchronous operation means one constructor new Promise. By the way, you can automate this task. There's a function built into node.js that makes promises out of asynchronous functions:</p>
14
<p>You can also do this in the front end - just search for a package with the promisify function.</p>
14
<p>You can also do this in the front end - just search for a package with the promisify function.</p>
15
<p>In real life, some tasks have no asynchronous code, but they still need promises to build a chain.</p>
15
<p>In real life, some tasks have no asynchronous code, but they still need promises to build a chain.</p>
16
<p>You can make that promise yourself:</p>
16
<p>You can make that promise yourself:</p>
17
<p>It's the same for a promise that ends unsuccessfully:</p>
17
<p>It's the same for a promise that ends unsuccessfully:</p>
18
<p>For these tasks, there are abbreviations that make the code look cleaner:</p>
18
<p>For these tasks, there are abbreviations that make the code look cleaner:</p>
19
<h2>Device</h2>
19
<h2>Device</h2>
20
<p>Technically, a promise is an object that has three states:</p>
20
<p>Technically, a promise is an object that has three states:</p>
21
<ul><li>Pending</li>
21
<ul><li>Pending</li>
22
<li>Fulfilled</li>
22
<li>Fulfilled</li>
23
<li>Rejected</li>
23
<li>Rejected</li>
24
</ul><p>A promise starts in the pending state. Then, it uses functions resolve and reject, and translated into one of the final states - fulfilled or rejected. In these states, the promise cannot be rolled back or changed to another terminal state.</p>
24
</ul><p>A promise starts in the pending state. Then, it uses functions resolve and reject, and translated into one of the final states - fulfilled or rejected. In these states, the promise cannot be rolled back or changed to another terminal state.</p>
25
<p>In other words, after calling resolve there's no way to make the promise have the rejected state by calling the reject function.</p>
25
<p>In other words, after calling resolve there's no way to make the promise have the rejected state by calling the reject function.</p>