You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/11-async/02-promise-basics/article.md
+8-8
Original file line number
Diff line number
Diff line change
@@ -31,9 +31,9 @@ When the executor obtains the result, be it soon or late - doesn't matter, it sh
31
31
-`resolve(value)` — if the job finished successfully, with result `value`.
32
32
-`reject(error)` — if an error occurred, `error` is the error object.
33
33
34
-
So to summarize: the executor runs automatically, it should do a job, and then call either `resolve` or `reject`.
34
+
So to summarize: the executor runs automatically and performs a job. Then it should call `resolve`if it was succssful or `reject` if there was an error.
35
35
36
-
The `promise` object returned by `new Promise` constructor has internal properties:
36
+
The `promise` object returned by the `new Promise` constructor has internal properties:
37
37
38
38
-`state` — initially `"pending"`, then changes to either `"fulfilled"` when `resolve` is called or `"rejected"` when `reject` is called.
39
39
-`result` — initially `undefined`, then changes to `value` when `resolve(value)` called or `error` when `reject(error)` is called.
@@ -58,7 +58,7 @@ let promise = new Promise(function(resolve, reject) {
58
58
We can see two things by running the code above:
59
59
60
60
1. The executor is called automatically and immediately (by `new Promise`).
61
-
2. The executor receives two arguments: `resolve` and `reject` — these functions are pre-defined by the JavaScript engine. So we don't need to create them. We should only call one of them when ready.
61
+
2. The executor receives two arguments: `resolve` and `reject`. These functions are pre-defined by the JavaScript engine, so we don't need to create them. We should only call one of them when ready.
62
62
63
63
After one second of "processing" the executor calls `resolve("done")` to produce the result. This changes the state of the `promise` object:
64
64
@@ -79,7 +79,7 @@ The call to `reject(...)` moves the promise object to `"rejected"` state:
79
79
80
80

81
81
82
-
To summarize, the executor should do a job (something that takes time usually) and then call `resolve` or `reject` to change the state of the corresponding promise object.
82
+
To summarize, the executor should perform a job (usually something that takes time) and then call `resolve` or `reject` to change the state of the corresponding promise object.
83
83
84
84
A promise that is either resolved or rejected is called "settled", as opposed to an initially "pending" promise.
85
85
@@ -166,7 +166,7 @@ promise.then(
166
166
167
167
The first function was executed.
168
168
169
-
And in the case of a rejection -- the second one:
169
+
And in the case of a rejection, the second one:
170
170
171
171
```js run
172
172
let promise = new Promise(function(resolve, reject) {
@@ -205,7 +205,7 @@ let promise = new Promise((resolve, reject) => {
205
205
});
206
206
207
207
*!*
208
-
// .catch(f) is the same as .then(null, f)
208
+
// .catch(f) is the same as promise.then(null, f)
209
209
promise.catch(alert); // shows "Error: Whoops!" after 1 second
210
210
*/!*
211
211
```
@@ -255,7 +255,7 @@ It's not exactly an alias of `then(f,f)` though. There are several important dif
255
255
})
256
256
.finally(() => alert("Promise ready"))
257
257
.catch(err => alert(err)); // <-- .catch handles the error object
258
-
```
258
+
```
259
259
260
260
That's very convenient, because `finally` is not meant to process a promise result. So it passes it through.
261
261
@@ -303,7 +303,7 @@ Let's rewrite it using Promises.
303
303
The new function `loadScript` will not require a callback. Instead, it will create and return a Promise object that resolves when the loading is complete. The outer code can add handlers (subscribing functions) to it using `.then`:
0 commit comments