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
@@ -229,11 +229,12 @@ new Promise((resolve, reject) => {
229
229
*!*
230
230
// runs when the promise is settled, doesn't matter successfully or not
231
231
.finally(() => stop loading indicator)
232
+
// so the loading indicator is always stopped before we process the result/error
232
233
*/!*
233
234
.then(result => show result, err => show error)
234
235
```
235
236
236
-
It's not exactly an alias of `then(f,f)` though. There are several important differences:
237
+
That said, `finally(f)` isn't exactly an alias of `then(f,f)` though. There are few subtle differences:
237
238
238
239
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.
239
240
2. A `finally` handler passes through results and errors to the next handler.
@@ -257,14 +258,13 @@ It's not exactly an alias of `then(f,f)` though. There are several important dif
257
258
.catch(err => alert(err)); // <-- .catch handles the error object
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
+
That's very convenient, because `finally` is not meant to process a promise result. So it passes it through.
261
262
262
-
We'll talk more about promise chaining and result-passing between handlers in the next chapter.
263
+
We'll talk more about promise chaining and result-passing between handlers in the next chapter.
263
264
264
-
3. Last, but not least, `.finally(f)` is a more convenient syntax than `.then(f, f)`: no need to duplicate the function `f`.
265
265
266
-
````smart header="On settled promises handlers run immediately"
267
-
If a promise is pending, `.then/catch/finally` handlers wait for it. Otherwise, if a promise has already settled, they execute immediately:
266
+
````smart header="We can attach handlers to settled promises"
267
+
If a promise is pending, `.then/catch/finally` handlers wait for it. Otherwise, if a promise has already settled, they just run:
268
268
269
269
```js run
270
270
// the promise becomes resolved immediately upon creation
@@ -273,9 +273,9 @@ let promise = new Promise(resolve => resolve("done!"));
273
273
promise.then(alert); // done! (shows up right now)
274
274
```
275
275
276
-
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.
276
+
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.
277
277
278
-
Promises are more flexible. We can add handlers any time: if the result is already there, our handlers get it immediately.
278
+
Promises are more flexible. We can add handlers any time: if the result is already there, they just execute.
279
279
````
280
280
281
281
Next, let's see more practical examples of how promises can help us write asynchronous code.
0 commit comments