From 07ae5580253feea2e7db1af8e00547ee736d5761 Mon Sep 17 00:00:00 2001 From: Jonathan Chue <5141427+jchue@users.noreply.github.com> Date: Sat, 7 Dec 2019 16:43:10 -0800 Subject: [PATCH 1/2] Make minor grammar corrections/updates to async/promise-chaining --- 1-js/11-async/03-promise-chaining/article.md | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/1-js/11-async/03-promise-chaining/article.md b/1-js/11-async/03-promise-chaining/article.md index e4609b8e..00c1ab17 100644 --- a/1-js/11-async/03-promise-chaining/article.md +++ b/1-js/11-async/03-promise-chaining/article.md @@ -1,7 +1,7 @@ # Promises chaining -Let's return to the problem mentioned in the chapter : we have a sequence of asynchronous tasks to be done one after another. For instance, loading scripts. How can we code it well? +Let's return to the problem mentioned in the chapter : we have a sequence of asynchronous tasks to be performed one after another — for instance, loading scripts. How can we code it well? Promises provide a couple of recipes to do that. @@ -164,7 +164,7 @@ loadScript("/article/promise-chaining/one.js") Here each `loadScript` call returns a promise, and the next `.then` runs when it resolves. Then it initiates the loading of the next script. So scripts are loaded one after another. -We can add more asynchronous actions to the chain. Please note that the code is still "flat", it grows down, not to the right. There are no signs of "pyramid of doom". +We can add more asynchronous actions to the chain. Please note that the code is still "flat" — it grows down, not to the right. There are no signs of the "pyramid of doom". Technically, we could add `.then` directly to each `loadScript`, like this: @@ -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 of 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. @@ -319,13 +319,9 @@ fetch('/article/promise-chaining/user.json') .then(githubUser => alert(`Finished showing ${githubUser.name}`)); ``` -That is, `.then` handler in line `(*)` now returns `new Promise`, that becomes settled only after the call of `resolve(githubUser)` in `setTimeout` `(**)`. +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. -The next `.then` in 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: From 95ddbe45a72fc2d19f37b45f91121b9937381b1e Mon Sep 17 00:00:00 2001 From: Jonathan Chue <5141427+jchue@users.noreply.github.com> Date: Thu, 2 Jan 2020 12:41:00 -0800 Subject: [PATCH 2/2] Update as per @paroche's comments --- 1-js/11-async/03-promise-chaining/article.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1-js/11-async/03-promise-chaining/article.md b/1-js/11-async/03-promise-chaining/article.md index 00c1ab17..da6e9782 100644 --- a/1-js/11-async/03-promise-chaining/article.md +++ b/1-js/11-async/03-promise-chaining/article.md @@ -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. @@ -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. -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: