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
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.
166
166
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".
168
168
169
169
Technically, we could add `.then` directly to each `loadScript`, like this:
170
170
@@ -189,9 +189,9 @@ Sometimes it's ok to write `.then` directly, because the nested function has acc
189
189
190
190
191
191
````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.
193
193
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`.
195
195
196
196
Here's an example of a thenable object:
197
197
@@ -216,7 +216,7 @@ new Promise(resolve => resolve(1))
216
216
.then(alert); // shows 2 after 1000ms
217
217
```
218
218
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.
220
220
221
221
This feature allows us to integrate custom objects with promise chains without having to inherit from `Promise`.
222
222
````
@@ -234,7 +234,7 @@ let promise = fetch(url);
234
234
235
235
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*.
236
236
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.
238
238
239
239
The code below makes a request to `user.json` and loads its text from the server:
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.
291
291
292
292
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.
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``(**)`.
323
323
324
324
The next `.then` in chain will wait for that.
325
325
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.
327
327
328
328
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.
0 commit comments