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: 8-async/03-promise-chaining/article.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -637,7 +637,9 @@ In theory, nothing should happen. In case of an error happens, the promise state
637
637
638
638
In practice, that means that the code is bad. Indeed, how come that there's no error handling?
639
639
640
-
Most JavaScript engines track such situations and generate a global error in that case. In the browser we can catch it using the event `unhandledrejection`:
640
+
Most JavaScript engines track such situations and generate a global error in that case. We can see it in the console.
641
+
642
+
In the browser we can catch it using the event `unhandledrejection`:
Copy file name to clipboardExpand all lines: 8-async/04-promise-api/article.md
+40-17Lines changed: 40 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,29 +71,48 @@ The syntax is:
71
71
let promise =Promise.all(iterable);
72
72
```
73
73
74
-
It takes an `iterable` object with promises, for instance an array and returns a new promise that resolves with when all of them are settled and has an array of results.
74
+
It takes an `iterable` object with promises, technically it can be any iterable, but usually it's an array, and returns a new promise. The new promise resolves with when all of them are settled and has an array of their results.
75
75
76
-
For instance, the `Promise.all` below settles after 3 seconds, and the result is an array `[1, 2, 3]`:
76
+
For instance, the `Promise.all` below settles after 3 seconds, and then its result is an array `[1, 2, 3]`:
]).then(alert); // 1,2,3 when promises are ready: each promise contributes an array member
84
84
```
85
85
86
86
Please note that the relative order is the same. Even though the first promise takes the longest time to resolve, it is still first in the array of results.
87
87
88
-
A more real-life example with fetching user information for an array of github users by their names:
88
+
A common trick is to map an array of job data into an array of promises, and then wrap that into `Promise.all`.
89
+
90
+
For instance, if we have an array of URLs, we can fetch them all like this:
89
91
90
92
```js run
91
-
let names = ['iliakan', 'remy', 'jresig'];
93
+
let urls = [
94
+
'https://api.github.com/users/iliakan',
95
+
'https://api.github.com/users/remy',
96
+
'https://api.github.com/users/jeresig'
97
+
];
92
98
93
99
// map every url to the promise fetch(github url)
94
-
let requests =names.map(name=>fetch(`https://api.github.com/users/${name}`));
A more real-life example with fetching user information for an array of github users by their names (or we could fetch an array of goods by their ids, the logic is same):
110
+
111
+
```js run
112
+
let names = ['iliakan', 'remy', 'jeresig'];
113
+
114
+
let requests =names.map(name=>fetch(`https://api.github.com/users/${name}`));
115
+
97
116
Promise.all(requests)
98
117
.then(responses=> {
99
118
// all responses are ready, we can show HTTP status codes
@@ -103,13 +122,13 @@ Promise.all(requests)
103
122
104
123
return responses;
105
124
})
106
-
// map array of responses into array of response.json() and wrap them info Promise.all
125
+
// map array of responses into array of response.json() to read their content
If any of the promises is rejected, `Promise.all`also rejects with that error.
131
+
If any of the promises is rejected, `Promise.all`immediately rejects with that error.
113
132
114
133
For instance:
115
134
@@ -124,12 +143,14 @@ Promise.all([
124
143
]).catch(alert); // Error: Whoops!
125
144
```
126
145
127
-
Here the `.catch` runs after 2 seconds, when the second promise rejects, and the rejection error becomes the outcome of the whole `Promise.all`.
146
+
Here the second promise rejects in two seconds. That leads to immediate rejection of `Promise.all`, so `.catch` executes: the rejection error becomes the outcome of the whole `Promise.all`.
147
+
148
+
The important detail is that promises provide no way to "cancel" or "abort" their execution. So other promises continue to execute, and the eventually settle, but all their results are ignored.
128
149
129
-
The important detail is that promises provide no way to "cancel" or "abort" their execution. So in the example above all promises finally settle, but in case of an error all results are ignored, "thrown away".
150
+
There are ways to avoid this: we can either write additional code to `clearTimeout` (or otherwise cancel) the promises in case of an error, or we can make errors show up as members in the resulting array (see the task below this chapter about it).
130
151
131
-
````smart header="`Promise.all` wraps non-promise arguments into `Promise.resolve`"
132
-
Normally, `Promise.all(iterable)` accepts an iterable of promise objects. But if any of those objects is not a promise, it's wrapped in `Promise.resolve`.
152
+
````smart header="`Promise.all(iterable)` allows non-promise items in `iterable`"
153
+
Normally, `Promise.all(iterable)` accepts an iterable (in most cases an array) of promises. But if any of those objects is not a promise, it's wrapped in `Promise.resolve`.
133
154
134
155
For instance, here the results are `[1, 2, 3]`:
135
156
@@ -167,7 +188,7 @@ Promise.race([
167
188
]).then(alert); // 1
168
189
```
169
190
170
-
So, the first result/error becomes the result of the whole `Promise.race`, and further results/errors are ignored.
191
+
So, the first result/error becomes the result of the whole `Promise.race`. After the first settled promise "wins the race", all further results/errors are ignored.
171
192
172
193
## Summary
173
194
@@ -177,3 +198,5 @@ There are 4 static methods of `Promise` class:
177
198
2. `Promise.reject(error)` -- makes a rejected promise with the given error,
178
199
3. `Promise.all(promises)` -- waits for all promises to resolve and returns an array of their results. If any of the given promises rejects, then it becomes the error of `Promise.all`, and all other results are ignored.
179
200
4. `Promise.race(promises)` -- waits for the first promise to settle, and its result/error becomes the outcome.
201
+
202
+
Of these four, `Promise.all` is the most common in practice.
0 commit comments