Skip to content

Commit 77eedd8

Browse files
authored
Merge pull request #1538 from hrodward/patch-34
Update article.md
2 parents 8d45a7c + 3e48d52 commit 77eedd8

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

1-js/11-async/03-promise-chaining/article.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ In practice we rarely need multiple handlers for one promise. Chaining is used m
8686

8787
A handler, used in `.then(handler)` may create and return a promise.
8888

89-
In that case further handlers wait till it settles, and then get its result.
89+
In that case further handlers wait until it settles, and then get its result.
9090

9191
For instance:
9292

@@ -164,7 +164,7 @@ loadScript("/article/promise-chaining/one.js")
164164

165165
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.
166166

167-
We can add more asynchronous actions to the chain. Please note that code is still "flat", it grows down, not to the right. There are no signs of "pyramid of doom".
167+
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".
168168

169169
Technically, we could add `.then` directly to each `loadScript`, like this:
170170

@@ -189,9 +189,9 @@ Sometimes it's ok to write `.then` directly, because the nested function has acc
189189

190190

191191
````smart header="Thenables"
192-
To be precise, a handler may return not exactly a promise, but a so-called "thenable" object - an arbitrary object that has method `.then`, and it will be treated the same way as a promise.
192+
To be precise, a handler may return not exactly a promise, but a so-called "thenable" object - an arbitrary object that has a method `.then`. It will be treated the same way as a promise.
193193
194-
The idea is that 3rd-party libraries may implement "promise-compatible" objects of their own. They can have extended set of methods, but also be compatible with native promises, because they implement `.then`.
194+
The idea is that 3rd-party libraries may implement "promise-compatible" objects of their own. They can have an extended set of methods, but also be compatible with native promises, because they implement `.then`.
195195
196196
Here's an example of a thenable object:
197197
@@ -216,7 +216,7 @@ new Promise(resolve => resolve(1))
216216
.then(alert); // shows 2 after 1000ms
217217
```
218218
219-
JavaScript checks the object returned by `.then` handler in the line `(*)`: if it has a callable method named `then`, then it calls that method providing native functions `resolve`, `reject` as arguments (similar to executor) and waits until one of them is called. In the example above `resolve(2)` is called after 1 second `(**)`. Then the result is passed further down the chain.
219+
JavaScript checks the object returned by the `.then` handler in line `(*)`: if it has a callable method named `then`, then it calls that method providing native functions `resolve`, `reject` as arguments (similar to an executor) and waits until one of them is called. In the example above `resolve(2)` is called after 1 second `(**)`. Then the result is passed further down the chain.
220220
221221
This feature allows us to integrate custom objects with promise chains without having to inherit from `Promise`.
222222
````
@@ -234,7 +234,7 @@ let promise = fetch(url);
234234

235235
This makes a network request to the `url` and returns a promise. The promise resolves with a `response` object when the remote server responds with headers, but *before the full response is downloaded*.
236236

237-
To read the full response, we should call a method `response.text()`: it returns a promise that resolves when the full text downloaded from the remote server, with that text as a result.
237+
To read the full response, we should call the method `response.text()`: it returns a promise that resolves when the full text is downloaded from the remote server, with that text as a result.
238238

239239
The code below makes a request to `user.json` and loads its text from the server:
240240

@@ -265,7 +265,7 @@ fetch('/article/promise-chaining/user.json')
265265

266266
Now let's do something with the loaded user.
267267

268-
For instance, we can make one more request to GitHub, load the user profile and show the avatar:
268+
For instance, we can make one more requests to GitHub, load the user profile and show the avatar:
269269

270270
```js run
271271
// Make a request for user.json
@@ -287,7 +287,7 @@ fetch('/article/promise-chaining/user.json')
287287
});
288288
```
289289

290-
The code works, see comments about the details. Although, there's a potential problem in it, a typical error of those who begin to use promises.
290+
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.
291291

292292
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.
293293

@@ -319,11 +319,11 @@ fetch('/article/promise-chaining/user.json')
319319
.then(githubUser => alert(`Finished showing ${githubUser.name}`));
320320
```
321321

322-
That is, `.then` handler in the line `(*)` now returns `new Promise`, that becomes settled only after the call of `resolve(githubUser)` in `setTimeout` `(**)`.
322+
That is, `.then` handler in line `(*)` now returns `new Promise`, that becomes settled only after the call of `resolve(githubUser)` in `setTimeout` `(**)`.
323323

324324
The next `.then` in chain will wait for that.
325325

326-
As a good rule, an asynchronous action should always return a promise.
326+
As a good practice, an asynchronous action should always return a promise.
327327

328328
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.
329329

0 commit comments

Comments
 (0)