Skip to content

Commit 6f446cc

Browse files
committed
PR comment corrections, minus last two
1 parent cb806aa commit 6f446cc

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

6-async/02-promise-basics/article.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ Everyone is happy: you, because the people don't crowd you any more, and fans, b
99
This is a real-life analogy for things we often have in programming:
1010

1111
1. A "producing code" that does something and takes time. For instance, the code loads a remote script. That's a "singer".
12-
2. A "consuming code" that wants the result of the "producing code" once it's ready. Many functions in your "consuming code" may need that result. These are "fans".
13-
3. A *promise* is a special JavaScript object that links the "producing code" and the "consuming code" together. In terms of our analogy: this is the "subscription list". The "producing code" takes the time it needs to produce the promised result, and the "promise" makes it available to all of the subscribed code when it's ready.
12+
2. A "consuming code" that wants the result of the "producing code" once it's ready. Many functions (in your "consuming code") may need that result. These are the "fans".
13+
3. A *promise* is a special JavaScript object that links the "producing code" and the "consuming code" together. In terms of our analogy: this is the "subscription list". The "producing code" takes whatever time it needs to produce the promised result, and the "promise" makes that result available to all of the subscribed code when it's ready.
1414

1515
The analogy isn't terribly accurate, because JavaScript promises are more complex than a simple subscription list: they have additional features and limitations. But this will serve for an introduction.
1616

@@ -22,7 +22,7 @@ let promise = new Promise(function(resolve, reject) {
2222
});
2323
```
2424

25-
The function passed to `new Promise` is called the *executor*. When the promise is created, this executor function is called (run) automatically. It contains the producing code, that should eventually produce a result. In terms of the analogy above: the executor is the "singer".
25+
The function passed to `new Promise` is called the *executor*. When the promise is created, this executor function is called (or, run) automatically. It contains the producing code, that should eventually produce a result. In terms of the analogy above: the executor is the "singer".
2626

2727
The resulting `promise` object has internal properties:
2828

@@ -57,7 +57,7 @@ let promise = new Promise(function(resolve, reject) {
5757
We can see two things by running the code above:
5858

5959
1. The executor is called automatically and immediately (by the `new Promise`).
60-
2. The executor receives two arguments: `resolve` and `reject` — these functions are pre-defined. We don't need to create them. Instead, we should write the executor to call them when ready.
60+
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. Instead, we should write the executor to call them when ready.
6161

6262
After one second of "processing" the executor calls `resolve("done")` to produce the result:
6363

@@ -94,7 +94,7 @@ let promise = new Promise(function(resolve, reject) {
9494
});
9595
```
9696
97-
The idea is that a job done by the executor may have only one result or an error. (If you are familiar with other languages, you might be aware of data structures which allow many "flowing" results, like streams and queues. They have their own advantages and disadvantages as compared with Promises. But as they are not supported by the JavaScript core, we won't be covering them.)
97+
The idea is that a job done by the executor may have only one result or an error.
9898
9999
Further, `resolve`/`reject` expect only one argument and will ignore additional arguments.
100100
````
@@ -112,7 +112,7 @@ let promise = new Promise(function(resolve, reject) {
112112
});
113113
```
114114

115-
For instance, it might happen when we start to do a job but then see that everything has already been completed. That is fine: we have a resolved Promise immediately.
115+
For instance, this might happen when we start to do a job but then see that everything has already been completed. That is fine: we immediately have a resolved Promise.
116116
````
117117
118118
```smart header="The `state` and `result` are internal"
@@ -174,7 +174,7 @@ promise.then(
174174
);
175175
```
176176
177-
If we're interested only in successful Promises, then we can provide only one function argument to `.then`:
177+
If we're interested only in successful completions, then we can provide only one function argument to `.then`:
178178
179179
```js run
180180
let promise = new Promise(resolve => {
@@ -186,7 +186,7 @@ promise.then(alert); // shows "done!" after 1 second
186186
*/!*
187187
```
188188
189-
If we're interested only in errors, then we can use `.catch(errorHandlingFunction)`, which is just like doing `.then(null, errorHandlingFunction)`.
189+
If we're interested only in errors, then we can use `.catch(errorHandlingFunction)`, which is exactly the same as `.then(null, errorHandlingFunction)`.
190190
191191
192192
```js run
@@ -216,14 +216,14 @@ That's handy for jobs that may sometimes require time and sometimes finish immed
216216
````
217217

218218
````smart header="Handlers of `.then`/`.catch` are always asynchronous"
219-
Even when the Promise is immediately resolved, code which occurs on lines below your `.then`/`.catch` can still run first. When the JavaScript engine sees your Promise, it will not only run the executor (the "producing code"), it will also go ahead and start executing code which occurs after your Promise and its `.then`/`.catch` calls.
219+
Even when the Promise is immediately resolved, code which occurs on lines below your `.then`/`.catch` may still run first. When the JavaScript engine sees your Promise, it will not only run the executor (the "producing code"), it will also go ahead and start executing code which occurs after your Promise and its `.then`/`.catch` calls.
220220

221-
So, when it is time for the function passed to `.then`/`.catch` to execute (nearly immediately in the case of an immediately-resolved Promise), the JavaScript engine puts it into an internal execution queue which will then already have items to be run.
221+
So, when it is time for the function passed to `.then`/`.catch` to execute (nearly immediately in the case of an immediately-resolved Promise), the JavaScript engine puts it into an internal execution queue (which will then already have items to be run).
222222

223223
The JavaScript engine is trying to do as many things at virtually the same time as possible. Everything is racing to get done. So, in the example code below, behind the scenes, you can imagine that the events might occur in this order:
224224

225225
1. The new Promise object is constructed on Line 2.
226-
2. The subscriber on Line 4 (the call to `alert`) is be registered with the Promise.
226+
2. The subscriber on Line 4 (the call to `alert`) is registered with the Promise.
227227
3. The executor is run by the Promise object and immediately resolves.
228228
4. The other `alert` call, on Line 6, is executed.
229229
6. The Promise notices it has been resolved and therefore executes the registered call to `alert` from Line 4.

0 commit comments

Comments
 (0)