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: 1-js/11-async/05-promise-api/article.md
+16-14
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,9 @@ There are 5 static methods in the `Promise` class. We'll quickly cover their use
4
4
5
5
## Promise.all
6
6
7
-
Let's say we want to run many promises to execute in parallel, and wait until all of them are ready.
7
+
Let's say we want many promises to execute in parallel and wait until all of them are ready.
8
8
9
-
For instance, download several URLs in parallel and process the content when all are done.
9
+
For instance, download several URLs in parallel and process the content once they are all done.
10
10
11
11
That's what `Promise.all` is for.
12
12
@@ -18,7 +18,7 @@ let promise = Promise.all([...promises...]);
18
18
19
19
`Promise.all` takes an array of promises (it technically can be any iterable, but is usually an array) and returns a new promise.
20
20
21
-
The new promise resolves when all listed promises are settled and the array of their results becomes its result.
21
+
The new promise resolves when all listed promises are settled, and the array of their results becomes its result.
22
22
23
23
For instance, the `Promise.all` below settles after 3 seconds, and then its result is an array `[1, 2, 3]`:
24
24
@@ -89,12 +89,12 @@ Promise.all([
89
89
]).catch(alert); // Error: Whoops!
90
90
```
91
91
92
-
Here the second promise rejects in two seconds. That leads to an immediate rejection of `Promise.all`, so `.catch` executes: the rejection error becomes the outcome of the whole`Promise.all`.
92
+
Here the second promise rejects in two seconds. That leads to an immediate rejection of `Promise.all`, so `.catch` executes: the rejection error becomes the outcome of the entire`Promise.all`.
93
93
94
94
```warn header="In case of an error, other promises are ignored"
95
95
If one promise rejects, `Promise.all` immediately rejects, completely forgetting about the other ones in the list. Their results are ignored.
96
96
97
-
For example, if there are multiple `fetch` calls, like in the example above, and one fails, other ones will still continue to execute, but `Promise.all` won't watch them anymore. They will probably settle, but the result will be ignored.
97
+
For example, if there are multiple `fetch` calls, like in the example above, and one fails, the others will still continue to execute, but `Promise.all` won't watch them anymore. They will probably settle, but their results will be ignored.
98
98
99
99
`Promise.all` does nothing to cancel them, as there's no concept of "cancellation" in promises. In [another chapter](info:fetch-abort) we'll cover `AbortController` that can help with that, but it's not a part of the Promise API.
100
100
```
@@ -110,7 +110,7 @@ Promise.all([
110
110
setTimeout(() =>resolve(1), 1000)
111
111
}),
112
112
2,
113
-
3
113
+
3
114
114
]).then(alert); // 1, 2, 3
115
115
```
116
116
@@ -121,7 +121,7 @@ So we are able to pass ready values to `Promise.all` where convenient.
121
121
122
122
[recent browser="new"]
123
123
124
-
`Promise.all` rejects as a whole if any promise rejects. That's good for "all or nothing" cases, when we need *all* results to go on:
124
+
`Promise.all` rejects as a whole if any promise rejects. That's good for "all or nothing" cases, when we need *all* results successful to proceed:
125
125
126
126
```js
127
127
Promise.all([
@@ -131,7 +131,7 @@ Promise.all([
131
131
]).then(render); // render method needs results of all fetches
132
132
```
133
133
134
-
`Promise.allSettled` waits for all promises to settle. The resulting array has:
134
+
`Promise.allSettled` just waits for all promises to settle, regardless of the result. The resulting array has:
135
135
136
136
- `{status:"fulfilled", value:result}` for successful responses,
137
137
- `{status:"rejected", reason:error}` for errors.
@@ -169,7 +169,7 @@ The `results` in the line `(*)` above will be:
169
169
]
170
170
```
171
171
172
-
So, for each promise we get its status and `value/error`.
172
+
So for each promise we get its status and `value/error`.
173
173
174
174
### Polyfill
175
175
@@ -197,7 +197,7 @@ Now we can use `Promise.allSettled` to get the results of *all* given promises,
197
197
198
198
## Promise.race
199
199
200
-
Similar to `Promise.all`, but waits only for the first settled promise, and gets its result (or error).
200
+
Similar to `Promise.all`, but waits only for the first settled promise and gets its result (or error).
201
201
202
202
The syntax is:
203
203
@@ -222,9 +222,11 @@ The first promise here was fastest, so it became the result. After the first set
222
222
223
223
Methods `Promise.resolve` and `Promise.reject` are rarely needed in modern code, because `async/await` syntax (we'll cover it [a bit later](info:async-await)) makes them somewhat obsolete.
224
224
225
-
We cover them here for completeness, and for those who can't use `async/await` for some reason.
225
+
We cover them here for completeness and for those who can't use `async/await` for some reason.
226
226
227
-
- `Promise.resolve(value)` creates a resolved promise with the result `value`.
227
+
### Promise.resolve
228
+
229
+
`Promise.resolve(value)` creates a resolved promise with the result `value`.
228
230
229
231
Same as:
230
232
@@ -234,7 +236,7 @@ let promise = new Promise(resolve => resolve(value));
234
236
235
237
The method is used for compatibility, when a function is expected to return a promise.
236
238
237
-
For example, `loadCached` function below fetches a URL and remembers (caches) its content. For future calls with the same URL it immediately gets the previous content from cache, but uses `Promise.resolve` to make a promise of it, so that the returned value is always a promise:
239
+
For example, the `loadCached` function below fetches a URL and remembers (caches) its content. For future calls with the same URL it immediately gets the previous content from cache, but uses `Promise.resolve` to make a promise of it, so the returned value is always a promise:
238
240
239
241
```js
240
242
let cache = new Map();
@@ -259,7 +261,7 @@ We can write `loadCached(url).then(…)`, because the function is guaranteed to
259
261
260
262
### Promise.reject
261
263
262
-
- `Promise.reject(error)` creates a rejected promise with `error`.
264
+
`Promise.reject(error)` creates a rejected promise with `error`.
0 commit comments