Update as per @paroche's comments

This commit is contained in:
Jonathan Chue 2020-01-02 12:41:00 -08:00 committed by GitHub
parent 07ae558025
commit 95ddbe45a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -287,7 +287,7 @@ fetch('/article/promise-chaining/user.json')
}); });
``` ```
The code works; see comments about the details. However, there's a potential problem in it, a typical error of those who begin to use promises. The code works; see comments about the details. However, there's a potential problem in it, a typical error for those who begin to use promises.
Look at the line `(*)`: how can we do something *after* the avatar has finished showing and gets removed? For instance, we'd like to show a form for editing that user or something else. As of now, there's no way. Look at the line `(*)`: how can we do something *after* the avatar has finished showing and gets removed? For instance, we'd like to show a form for editing that user or something else. As of now, there's no way.
@ -321,7 +321,7 @@ fetch('/article/promise-chaining/user.json')
That is, the `.then` handler in line `(*)` now returns `new Promise`, that becomes settled only after the call of `resolve(githubUser)` in `setTimeout` `(**)`. The next `.then` in the chain will wait for that. That is, the `.then` handler in line `(*)` now returns `new Promise`, that becomes settled only after the call of `resolve(githubUser)` in `setTimeout` `(**)`. The next `.then` in the chain will wait for that.
As a good practice, an asynchronous action should always return a promise. That makes it possible to plan actions after it. Even if we don't plan to extend the chain now, we may need it later. As a good practice, an asynchronous action should always return a promise. That makes it possible to plan actions after it; even if we don't plan to extend the chain now, we may need it later.
Finally, we can split the code into reusable functions: Finally, we can split the code into reusable functions: