minor fixes, close #2137
This commit is contained in:
parent
165ea8373c
commit
e9686467e6
1 changed files with 8 additions and 8 deletions
|
@ -229,11 +229,12 @@ new Promise((resolve, reject) => {
|
||||||
*!*
|
*!*
|
||||||
// runs when the promise is settled, doesn't matter successfully or not
|
// runs when the promise is settled, doesn't matter successfully or not
|
||||||
.finally(() => stop loading indicator)
|
.finally(() => stop loading indicator)
|
||||||
|
// so the loading indicator is always stopped before we process the result/error
|
||||||
*/!*
|
*/!*
|
||||||
.then(result => show result, err => show error)
|
.then(result => show result, err => show error)
|
||||||
```
|
```
|
||||||
|
|
||||||
It's not exactly an alias of `then(f,f)` though. There are several important differences:
|
That said, `finally(f)` isn't exactly an alias of `then(f,f)` though. There are few subtle differences:
|
||||||
|
|
||||||
1. A `finally` handler has no arguments. In `finally` we don't know whether the promise is successful or not. That's all right, as our task is usually to perform "general" finalizing procedures.
|
1. A `finally` handler has no arguments. In `finally` we don't know whether the promise is successful or not. That's all right, as our task is usually to perform "general" finalizing procedures.
|
||||||
2. A `finally` handler passes through results and errors to the next handler.
|
2. A `finally` handler passes through results and errors to the next handler.
|
||||||
|
@ -261,10 +262,9 @@ It's not exactly an alias of `then(f,f)` though. There are several important dif
|
||||||
|
|
||||||
We'll talk more about promise chaining and result-passing between handlers in the next chapter.
|
We'll talk more about promise chaining and result-passing between handlers in the next chapter.
|
||||||
|
|
||||||
3. Last, but not least, `.finally(f)` is a more convenient syntax than `.then(f, f)`: no need to duplicate the function `f`.
|
|
||||||
|
|
||||||
````smart header="On settled promises handlers run immediately"
|
````smart header="We can attach handlers to settled promises"
|
||||||
If a promise is pending, `.then/catch/finally` handlers wait for it. Otherwise, if a promise has already settled, they execute immediately:
|
If a promise is pending, `.then/catch/finally` handlers wait for it. Otherwise, if a promise has already settled, they just run:
|
||||||
|
|
||||||
```js run
|
```js run
|
||||||
// the promise becomes resolved immediately upon creation
|
// the promise becomes resolved immediately upon creation
|
||||||
|
@ -273,9 +273,9 @@ let promise = new Promise(resolve => resolve("done!"));
|
||||||
promise.then(alert); // done! (shows up right now)
|
promise.then(alert); // done! (shows up right now)
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that this is different, and more powerful than the real life "subscription list" scenario. If the singer has already released their song and then a person signs up on the subscription list, they probably won't receive that song. Subscriptions in real life must be done prior to the event.
|
Note that this makes promises more powerful than the real life "subscription list" scenario. If the singer has already released their song and then a person signs up on the subscription list, they probably won't receive that song. Subscriptions in real life must be done prior to the event.
|
||||||
|
|
||||||
Promises are more flexible. We can add handlers any time: if the result is already there, our handlers get it immediately.
|
Promises are more flexible. We can add handlers any time: if the result is already there, they just execute.
|
||||||
````
|
````
|
||||||
|
|
||||||
Next, let's see more practical examples of how promises can help us write asynchronous code.
|
Next, let's see more practical examples of how promises can help us write asynchronous code.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue