Make minor grammar corrections/updates to async/promise-basics
This commit is contained in:
parent
5b195795da
commit
4c650b54b7
1 changed files with 9 additions and 9 deletions
|
@ -22,7 +22,7 @@ let promise = new Promise(function(resolve, reject) {
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
The function passed to `new Promise` is called the *executor*. When `new Promise` is created, it runs automatically. It contains the producing code, that should eventually produce a result. In terms of the analogy above: the executor is the "singer".
|
The function passed to `new Promise` is called the *executor*. When `new Promise` is created, it runs automatically. It contains the producing code, that should eventually produce a result. In terms of the analogy above, the executor is the "singer".
|
||||||
|
|
||||||
Its arguments `resolve` and `reject` are callbacks provided by JavaScript itself. Our code is only inside the executor.
|
Its arguments `resolve` and `reject` are callbacks provided by JavaScript itself. Our code is only inside the executor.
|
||||||
|
|
||||||
|
@ -31,9 +31,9 @@ When the executor obtains the result, be it soon or late - doesn't matter, it sh
|
||||||
- `resolve(value)` — if the job finished successfully, with result `value`.
|
- `resolve(value)` — if the job finished successfully, with result `value`.
|
||||||
- `reject(error)` — if an error occurred, `error` is the error object.
|
- `reject(error)` — if an error occurred, `error` is the error object.
|
||||||
|
|
||||||
So to summarize: the executor runs automatically, it should do a job and then call either `resolve` or `reject`.
|
So to summarize: the executor runs automatically. It performs a job and then calls either `resolve` or `reject`.
|
||||||
|
|
||||||
The `promise` object returned by `new Promise` constructor has internal properties:
|
The `promise` object returned by the `new Promise` constructor has internal properties:
|
||||||
|
|
||||||
- `state` — initially `"pending"`, then changes to either `"fulfilled"` when `resolve` is called or `"rejected"` when `reject` is called.
|
- `state` — initially `"pending"`, then changes to either `"fulfilled"` when `resolve` is called or `"rejected"` when `reject` is called.
|
||||||
- `result` — initially `undefined`, then changes to `value` when `resolve(value)` called or `error` when `reject(error)` is called.
|
- `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) {
|
||||||
We can see two things by running the code above:
|
We can see two things by running the code above:
|
||||||
|
|
||||||
1. The executor is called automatically and immediately (by `new Promise`).
|
1. The executor is called automatically and immediately (by `new Promise`).
|
||||||
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.
|
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.
|
||||||
|
|
||||||
After one second of "processing" the executor calls `resolve("done")` to produce the result. This changes the state of the `promise` object:
|
After one second of "processing" the executor calls `resolve("done")` to produce the result. This changes the state of the `promise` object:
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ The call to `reject(...)` moves the promise object to `"rejected"` state:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
A promise that is either resolved or rejected is called "settled", as opposed to an initially "pending" promise.
|
A promise that is either resolved or rejected is called "settled", as opposed to an initially "pending" promise.
|
||||||
|
|
||||||
|
@ -166,7 +166,7 @@ promise.then(
|
||||||
|
|
||||||
The first function was executed.
|
The first function was executed.
|
||||||
|
|
||||||
And in the case of a rejection -- the second one:
|
And in the case of a rejection, the second one:
|
||||||
|
|
||||||
```js run
|
```js run
|
||||||
let promise = new Promise(function(resolve, reject) {
|
let promise = new Promise(function(resolve, reject) {
|
||||||
|
@ -205,7 +205,7 @@ let promise = new Promise((resolve, reject) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
*!*
|
*!*
|
||||||
// .catch(f) is the same as .then(null, f)
|
// .catch(f) is the same as promise.then(null, f)
|
||||||
promise.catch(alert); // shows "Error: Whoops!" after 1 second
|
promise.catch(alert); // shows "Error: Whoops!" after 1 second
|
||||||
*/!*
|
*/!*
|
||||||
```
|
```
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue